Using `GCCs` pre-processor as an assembler

assembly, c, compiler-construction, gcc

Solution

I think that XY Problem is a wrong description. The question is more "Concept A is needed to evaluate Concept B".

Concept A: What is an assembler?

See: Assemblers and Loader, by David Solomon. [some pearls of wisdom, some archaic trivia]

I very quickly discovered the lack of literature in this field. In strict contrast to compilers, for which a wide range of literature exists, very little has ever been written on assemblers and loaders.

An assembler consists of,

- A Symbol table to facilitates linking through some object format.

- Lexer and Parser for converting the text to a data structure or directly to machine code.

- Does 2 passes for most efficient branch and sub-routine calling.

- An opcode table.

An assembler is generally a `1-1` translation. However, often several variants of branches and calls will exist; generally known as long and short version. The opcode used will depend on the distance to the destination; a two pass compiler is needed to optimize forward branches.Alluded to by Harold

Concept B: Using the 'C' pre-processor as an assembler.

The best a 'C' pre-processor could emulate is a 1-pass assembler. A large class of CPU/instructions can be encoded like this; although the macros could be cumbersome. There would be no listings or xrefs, but most people would not miss those features. Also, the syntax would be odd due to limitation of the pre-processor. It would be difficult dealing with address fix-ups as labels would either re-use the 'C' symbol table by using pointers or a hand coded `#define` for the label offset. This limits this approach to anything but a basic block.

Large assembler Routines

Large assembler routines such as YUV/RGB transforms or MP3 decoding are highly unlikely to be used this way.

Multi-arch code

Multiple architecture code is quite common. For example an ARM wifi chip may have it's code embedded in a Linux kernel as firmware. It is possible that this technique could be useful here. However, using separate compilers/assembler for the different architectures and then using `objcopy` to embedded them is far more sane.

Self-modifying Code

This is probably the most useful. In fact many tools, such as linkers and loaders have high level functions which patch code at run time. It could also be used to conditionally change a routine at runtime; function pointers are almost as fast and easier to understand, not to mention the cache coherency issues.

See also: Gold Blog, by Ian Lance Taylor. [although he uses `<templates>`]

Problem

There are various open source assemblers such as gas, nasm, and yasm. They have different `pseudo-ops` and `macro` syntaxes. For many open source projects, assembler is pre-processed to replace constants and platform conditionals. What limitations would `gcc` have creating assembler assuming you can use all current `attributes` and `#pragmas`, excluding translation performance (compile/assemble to binary time)? I am not talking about inline-assembly. ``` #define MOV(RA,RB) (OXFEB10000UL | RA << 16 | RB) #define ADD(RA,RB) (OXFEB20000UL | RA << 16 | RB) #define RET (OXFEB7ABCDUL) unsigned long add4[] __attribute(section(".text")) = { ADD(R0,R1), ADD(R2,R3), MOV(R1,R2), ADD(R0,R1), RET() }; ``` I believe that using pointer arithmetic can allow simulation of `.` and other `labels`. Perhaps this is an XY problem; I am trying to understand why there are so many assemblers at all. It seems like everything can be done by the pre-processor and the assembler is really a programmer preference; or there is a technical limitation I am missing. I guess this might be related to 'Something you can do with an assembler that you can't do with shell code'. Edit: I have re-tagged this from C to compiler. I am interested in the technical details of an assembler. Is it simply a `1-1` translation and emitting relocations (as a compiler will) or is there more? I don't mean for people to code assembler as I have outlined above. I am trying to understand what the assemblers are doing. I don't believe there is a Dragon book for assemblers. Of course, the pre-processor can not create a `binary` by itself and needs additional machinery; it only translates text.

Original source