sysprog21/shecc

Not enough register for reading expression

eecheng87 opened this issue · 2 comments

There's a test case:

a = 1 + (2 + (3 + (4 + (5 + (6 + (7 + (8 + (9 + (10 + (11 + (12 + (13 + (14 + (15 + (16 + (17 + (18 + (19 + 20))))))))))))))))))

In shecc, it will store all constants in register. Obviously, there's not enough register.
In modern compiler, I found they just load an immediate. e.g.

li a4, 210

Even I mix identifier into expression, modern compiler still load an immediate value. Do we need to rewrite read expression? Maybe we need to evaluate expression before run time. Also, I think we need symbol table to store these identifier.

jserv commented

An effective evaluator is required for parsed expression. Which parts of C front-end would be modified to fit?

Maybe I need to modify read_expr which also contains read_operand (most complicate part).
It seems modern compiler will also evaluate result of function in expression during compile time, should we follow this? Following is example:

int f(int b){
	int c = 10;
	return b * c + 1;
}
void _start(){
    int a;
    int c = 10;
    a = 1 + ( f(1) + (1 + (1 + (1 + (1 + (1 + (1 + (1 + (1 + (1 + (20 + (3 + (1 + (1 + (6 + (1 + (1 + (1 + 20))))))))))))))))));
    print(a);

corresponding asm

00010068 <_start>:
   10068:	400027b7          	lui	a5,0x40002
   1006c:	04b00713          	li	a4,75
   10070:	00e78023          	sb	a4,0(a5) # 40002000 <__global_pointer$+0x3fff0788>