DoctorWkt / acwj

A Compiler Writing Journey
GNU General Public License v3.0
10.55k stars 1.02k forks source link

Nasm code generator #55

Closed the-grue closed 1 year ago

the-grue commented 1 year ago

In case there is any interest, I am adding a code generation option that creates nasm code instead of gas as I am working through the project steps.

the-grue commented 1 year ago

That was fun.

DoctorWkt commented 1 year ago

the-grue, thanks for all the hard work. Question: what are all the *.n files? Are they necessary?

DoctorWkt commented 1 year ago

Ah, the .n files. So is there no way to tell nasm to let the linker deal with undefined global symbols? I just tried to add some code to cgn.c to print out the C_EXTERN symbols in the symbol table, but none of the symbols in the include/ directory files are marked as extern. Sigh. I really don't want to pollute the tree with dozens of .n files, especially when gas doesn't need them.

the-grue commented 1 year ago

Hi Warren! It doesn't get to the linking step as nasm doesn't seem to like the undefined externals. They can certainly be consolidated elsewhere but I haven't found a way to get around having them other than hard-coding them into the code generator, which isn't a good solution either. I can do some more work on it, I just didn't want to extend more effort if there wasn't any interest.

DoctorWkt commented 1 year ago

Thanks the-grue. We have to find a way so that cwn can be a general-purpose compiler, not just one that can compile itself. All C compilers can do this: cc -o hello hello.c without needing the programmer to also create a hello.n file first. Surely there has to be a way to tell nasm to ignore undefined symbols and let the linker take care of them.

the-grue commented 1 year ago

Of course. Let me see what I can do.

the-grue commented 1 year ago

Ok, please review the changes in the 62_Cleanup lesson. If this is acceptable, please let me know and I will correct the deficiencies in prior lessons. The solution was in the code and not trying to bend nasm into conforming. :)

Also open to other suggestions.

DoctorWkt commented 1 year ago

Looks good James, thanks!