Sh3b0 / cplus

Compiler for an imperative toy programming language
MIT License
4 stars 2 forks source link

Argv input doesn't work #7

Open Sh3b0 opened 2 years ago

Sh3b0 commented 2 years ago

Code to reproduce

routine main(x: integer) : integer is
    println x;
    return 0;
end
$ ./cplus -d source.cp
$ ./a.out 5

Expected output

Prints 5

Actual output

Prints other random value, although the generated ir.ll seems correct.

TonyDecvA180XN commented 2 years ago

In clang (and also others C++ compilers) the main function is linked against the CRT definition:

int main(int argc, char *argv[])

which is a number of arguments and array of strings, that are arguments themselves parsed by space. Linking process happens consequently and involves one-by-one check of ABI compatility. In our case, we expect the integer variable in both function signatures, which is fine, but missing second array of strings, which is also fine, surprisingly. That way, the variable x in the program is linked against argc. This can be demonstrated here: image P. S. Note that first argument is always name of executable, a.exe in my case.

Sh3b0 commented 2 years ago

Got it, my bad. Well, we obviously need to implement the required behavior without having to introduce arrays of character pointers in the language.

I suggest to fix this by internally calling main() with the appropriate arguments then.

TonyDecvA180XN commented 2 years ago

Yeah, but the problem of static typing does not allow us to do that. CMD Arguments are always captured as string, so we would need to embed string to int conversion code into out program, which is something I don't think we have right now. Also, for unified conversion we would need to embed all these other functions such as str to float, str to bool into CPlus.