-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCG.h
More file actions
48 lines (47 loc) · 1.52 KB
/
CG.h
File metadata and controls
48 lines (47 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/***************************************************************************
Code Generator
***************************************************************************/
/*-------------------------------------------------------------------------
Data Segment
-------------------------------------------------------------------------*/
int data_offset = 0;/* Initial offset*/
int data_location()/* Reserves a data location*/
{
return data_offset++;
}
/*-------------------------------------------------------------------------
Code Segment
-------------------------------------------------------------------------*/
int code_offset = 0;/* Initial offset*/
int gen_label() /* Returns current offset */
{
return code_offset;
}
int reserve_loc() /* Reserves a code location */
{
return code_offset++;
}
/* Generates code at current location */
void gen_code( enum code_ops operation, int arg )
{ code[code_offset].op
= operation;
code[code_offset++].arg = arg;
}
/* Generates code at a reserved location*/
void back_patch( int addr, enum code_ops operation, int arg )
{
code[addr].op = operation;
code[addr].arg = arg;
}
/*-------------------------------------------------------------------------
Print Code to stdio
-------------------------------------------------------------------------*/
void print_code()
{
int i = 0;
while (i < code_offset) {
printf("%3ld: %-10s%4ld\n",i,op_name[(int) code[i].op], code[i].arg );
i++;
}
}
/************************** End Code Generator **************************/