1

I'm learning compilers and creating a code generator for a simple language that deals with two types: characters and integers.

After the user input has been scanned by the scanner and then parsed by the parser, I get an AST representation of the input. I have made a code generation for an even simpler language which only processes expressions with integers, operators and variables.

However with this new language I sometimes get a subtree for a type declaration, like this:

(IS TYPE (x) (INT))

which says x is of type INT.

Should there be a case in my code generator which deals with these type declarations? Or is this simply for the semantic analyzer to type check, so I should just assume the types have been checked and ignore this part of the tree and simply assign the value for x?

1
  • It depends. Do any expressions involving x change behaviour based on the type of x? Can you generate better code knowing the type? etc.
    – user395760
    Nov 18, 2011 at 15:16

2 Answers 2

1

Both situations are possible, you need to describe more about your language, to see if you really need to add that feature to your code generator, or skip it as unnecessary, and avoid extra work with this difficult and interesting topic of designing a programming language.

Is you "code generator" a program that recieves as an input code in a programming language (maybe small one) and outputs code in another programming language (maybe small one) ?

This tool is usually called a "translator".

Is you "code generator" a program that receive as an input a programming language and outputs assembler / bytecode like programming language ?

This tool is usually called a "compiler".

Note: "pile" is a synonym for "stack".

Usually an A.S.T., stores the type of an operation, or function call. Example, in c:

...
int a = 3;
int b = 5;
float c = (float)(a * b);
...

The last line, generates an A.S.T. similar to this, (skip A.S.T. for other lines):

..................................................................
..................................................................
......................+--------------+............................
......................|    [root]    |............................
......................| (no type) =  |............................
......................+------+-------+............................
.............................|....................................
.................+-----------+------------+.......................
.................|........................|.......................
...........+-----+-----+....+-------------+-------------+.........
...........| (int) c   |....| (float) (cast operation)  |.........
...........+-----------+....+-------------+-------------+.........
..........................................|.......................
....................................+-----+-----+.................
....................................| (int) ()  |.................
....................................+-----+-----+.................  
..........................................|.......................
....................................+-----+-----+.................
....................................| (int) *   |.................
....................................+-----+-----+.................
..........................................|.......................
..............................+-----------+-----------+...........
..............................|.......................|...........
........................+-----+-----+...........+-----+-----+.....
........................| (int)  a  |...........| (float) b |.....
........................+-----------+...........+-----------+.....
..................................................................
..................................................................

Note that the "(float)" cast its like an operator or a function, similar to your question.

Good Luck.

1

If this is a declaration

(IS TYPE (x) (INT))

then x should be laid out in memory. In the case of C and automatic variables, local auto variables are allocated on stack. To allocate needed size of stack you should know sizes of all local vars and sizes are from types.

If this variable is stored in a register, you should select a register of needed size (think about x86 with: AL, AX, EAX, RAX - the same register with different sizes), if your target has such.

Also, type is needed when there is an ambiguous operation in AST, which can operate on different data sizes (e.g. char, short, int - or 8-bit, 16-bit, 32-bit, etc). And for some assemblers, size of data is encoded into instruction itself; so codegen should remember sizes of variables.

Or, if the type of operation was not recorded in AST, the ADD:

(ADD (x) (y))

may mean both float and int additions (ADD or FADD instructions), so types of x and y are needed in codegen to select right variant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.