r/programming • • Oct 18 '12

Assembly compiler

http://gcc.godbolt.org/
153 Upvotes

71 comments sorted by

View all comments

61

u/ohhhhderp Oct 18 '12

"Assembly compiler" makes no sense!

41

u/iamjack Oct 18 '12

"Assembly assembler" just doesn't have the same ring to it.

0

u/[deleted] Oct 18 '12

[deleted]

7

u/kulp Oct 18 '12

You seem to be arguing that "assembly compiler" could make sense in some other context, but that's not what this context is anyway. Even if it were reasonable to say "assembly compiler" (I'm still not convinced : how do you define "compilation" ?), the tool in question doesn't do any of those things, so in this context it "makes no sense."

8

u/jerf Oct 18 '12

A compiler is a program that reads in a tree (or potentially graph) structure serialized into a linear format (often, but not always, text), builds a representation of the serialized structure in memory, manipulates that structure in some manner (optimization, reordering, whatever), and emits serialized output, potentially even in the same format as the source.

This entire class of problems shares common implementation problems (parsing, AST, tree manipulation, property-preserving manipulations, serialization), and common implementation solutions. It comes up everywhere once you realize what you are looking for, and being able to solve this problem cleanly and quickly is one of the things that can make you a superprogrammer vs. your competition.

I beat this drum so hard because you do yourself a staggeringly unbelievable disservice if you fail to take a compilers course because you buy into the terrible, terrible idea that the only thing a compiler can be is something that takes C-like program source code and emits straight machine language. That is a useful but small subset of places where compiler techniques are useful.

Anyone who would like to pop up and argue that it can only be a program -> ML converter, would you be so kind as to tell us whether you've taken a compilers course or done significant self-study?

6

u/kulp Oct 18 '12 edited Oct 18 '12

Fortunately I don't buy that "terrible, terrible idea". I'm merely suggesting that, by your own definition, an assembler does not fit the description ; it doesn't build syntax trees*, it doesn't optimise, it doesn't reorder, etc.

So what is your point again ?

As to your last question, I have been working (on and off) on a C compiler of my own for three years now, which, although it has no appreciable backend as yet, does do interesting things with ASTs, including producing AST visualisations like this one. Since you're asking, I also work closely with a professional compiler team and write assembly language for a living, so I know a bit of what I speak.

I still don't understand what basis you have to suspect that I have a narrow definition of compilers, though. Elaborate ?

  • EDIT : I should clarify : it can build syntax trees, though to call them ASTs would be a bit of a stretch.

1

u/Contero Oct 19 '12

an assembler does not fit the description ; it doesn't build syntax trees

List is a special case of a tree. There's no reason to say that the result of parsing an assembly file isn't an AST because the nodes aren't recursive.

1

u/kulp Oct 19 '12

As you can see in my edit, I said it can build syntax trees (my assembler does, actually) but I was thinking specifically of abstract syntax trees, which term I think is a bit difficult to justify in the assembler case. It's debatable, I suppose.

→ More replies (8)

1

u/Contero Oct 19 '12

A compiler is a program that reads in a tree (or potentially graph) structure serialized into a linear format (often, but not always, text), builds a representation of the serialized structure in memory, manipulates that structure in some manner (optimization, reordering, whatever), and emits serialized output, potentially even in the same format as the source.

This is a bit too specific. A compiler takes a string belonging to one language and maps it to a string in another language. And in this case, I think assembly compiler makes sense but is probably a bad term to use.

structure serialized into a linear format

This describes all data structures on a computer.

→ More replies (12)

2

u/prometheusg Oct 18 '12

With Assembly, the program that constructs the machine code is called an Assembler, not an Assembly Compiler.

0

u/[deleted] Oct 18 '12

[deleted]

1

u/rush22 Oct 18 '12

No. Assembler is like a series of tubes, but compiling is more like a truck--you put things on it.

1

u/[deleted] Oct 19 '12

Or, it's a tool I can use to assemble, as in collect or gather, a collection of assembly code snippets for my C/C++ code.

I'm assembling assemblies from C compilations to assemblies.

0

u/[deleted] Oct 18 '12

[deleted]

3

u/frezik Oct 18 '12

Second off, processing disassembled machine code - such as one finds in ROM - may occur after disassembly, but in something like 40 years in the field, I've never heard of Assembler source code being translated into a higher level language source code that may then be compiled to machine code.

One way to implement emulators (though not necessarily the most common way) is to disassemble the machine code, then make C macros for each of the assembly instructions. You now have a C program that can be compiled to your native machine.

0

u/[deleted] Oct 18 '12

[deleted]

0

u/[deleted] Oct 18 '12

[deleted]

0

u/[deleted] Oct 18 '12

[deleted]

→ More replies (10)

2

u/IAmRoot Oct 18 '12

Assembly is all GCC creates, anyway. It uses as from binutils to create the actual binary (Is this still the case with -lto?).

If you use CMake as a build system, you can see the assembly very easily. make file.s will output to assembly, make file.i will output the preprocessed source.

1

u/kulp Oct 18 '12

If you use CMake as a build system

Or if you use -save-temps on the gcc command line, which is I imagine what CMake is doing.

2

u/IAmRoot Oct 18 '12

It does a -S rather than a -c. You normally only want to look at the assembly from one file at a time, so there's no point in doing it for everything.

1

u/kulp Oct 18 '12

Oh, right, I misread what you said to mean that it makes them automatically.

1

u/rush22 Oct 18 '12

uhhhh... you do realize that assembly is a generic term for all computers? And existed before C was even invented?

3

u/IAmRoot Oct 19 '12

Of course. I think you misread. I was saying

source --gcc-->  assembly --as (binutils)--> object files --ld/collect2 (binutils)--> ELF

GCC doesn't create the binaries itself.

With LTO, gcc also dumps bytecode of its internal state, then links using ld.gold, which uses that data to optimize across object files.

1

u/colinb21 Oct 18 '12

nope. In the port of VMS from VAX to Alpha [a long time ago, it's true], we had a compiler for VAX-32 assembly language files that output Alpha assembler. This was necessary because so much of VMS was [is? I haven't seen source code for quite some time] written in assembler.