-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenpcode.c
60 lines (59 loc) · 1.45 KB
/
genpcode.c
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
48
49
50
51
52
53
54
55
56
57
58
59
#include "genpcode.h"
#include <stdio.h>
#include <string.h>
const char oprtname[15][5]={"LIT", "LOD", "STO", "INT", "JMP", "JPC", "OPR","RED","WRI","LOD1","GTA","CAL","STO1","RED1"};
char *id;
int table_cnt=0,code_cnt=0,temp_num=0;
void enter(enum object k, enum object_t t){
if (k==variable||k==constant)
{
table_cnt++;
strcpy(table[table_cnt].name,id);
table[table_cnt].kind=k;
table[table_cnt].type=t;
table[table_cnt].level=level;
table[table_cnt].addr=var_cnt-1;//table_cnt-1;
table[table_cnt].value=temp_num;
temp_num=0;
}
else if (k==procedure)
{
table_cnt++;
strcpy(table[table_cnt].name,id);
table[table_cnt].kind=k;
table[table_cnt].type=t;
table[table_cnt].addr=code_cnt;
table[table_cnt].value=0;
table[table_cnt].level=level;
nowprocedure=table_cnt;
}
}
int position(char* tid){
strcpy(table[0].name,tid);//如果return的是0说明没找到
for(int i=table_cnt; i>=0;i--){
if (strcmp(table[i].name, tid)==0)
return i;
}
return -1;
}
int getv(int t){
if (t>0) return table[t].value;
return -1;
}
int getaddr(int t){
if (t>0) return table[t].addr;
return -1;
}
int gettype(int t){
if (t>0) return table[t].type;
return -1;
}
void gen(enum oprt f, int l, int a){
code[code_cnt].f=f;
code[code_cnt].l=l;
code[code_cnt++].a=a;
//printf("%8d:%s, %d, %d\n",code_cnt++, oprtname[f], l, a);
}
void printpcode(){
for (int i=0;i<code_cnt;i++) printf("%5d : %s %d %d\n",i,oprtname[code[i].f],code[i].l,code[i].a);
}