-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.c
More file actions
133 lines (104 loc) · 3.13 KB
/
code.c
File metadata and controls
133 lines (104 loc) · 3.13 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "globals.h"
#include "code.h"
#include "symtable.h"
/* TM location number for current instruction emission */
char labelTable[211][6];
static int emitLoc = 0 ;// the first location is for jump to main function adress
static int highEmitLoc = 0;/*the highest EmitLocation the instruction has been created */
static int labNum = 0;
/* Procedure emitComment prints a comment line
* with comment c in the code file
*
*/
void emitComment( char * c )
{ fprintf(code,"* %s\n",c);}
/* Procedure emitRO emits a register-only
* TM instruction
* op = the opcode
* r = target register
* s = 1st source register
* t = 2nd source register
* c = a comment to be printed if TraceCode is TRUE
*/
void emitRO( char *op, int r, int s, int t, char *c)
{
if (strcmp(op, "MOV") == 0 && (r == s)) {
return;//optimize
}
fprintf(code,"%3d: %5s %d,%d,%d ",emitLoc++,op,r,s,t);
fprintf(code,"\t%s",c) ;
fprintf(code,"\n") ;
if (highEmitLoc < emitLoc) highEmitLoc = emitLoc ;
} /* emitRO */
/* Procedure emitRM emits a register-to-memory
* TM instruction
* op = the opcode
* r = target register
* d = the offset
* s = the base register
* c = a comment to be printed if TraceCode is TRUE
*/
void emitRM( char * op, int r, int d, int s, char *c )
{
fprintf(code, "%3d: %5s %d,%d(%d) ", emitLoc++, op, r, d, s);
fprintf(code,"\t%s",c) ;
fprintf(code,"\n") ;
if (highEmitLoc < emitLoc) highEmitLoc = emitLoc ;
} /* emitRM */
void emitSYS(char * op, int r, int d, int s, char *c)
{
fprintf(code, "%3d: %s %d,%d(%d) ", emitLoc++, op, r, d, s);
fprintf(code, "\t%s", c);
fprintf(code, "\n");
if (highEmitLoc < emitLoc) highEmitLoc = emitLoc;
} /* emitSYS */
void emitLDCF(char * op, int r, float d, int s, char *c){
fprintf(code, "%3d: %5s %d,%f(%d)\n ", emitLoc++, op, r, d, s);
}
/* Function emitSkip skips "howMany" code
* locations for later backpatch. It also
* returns the current code position.
* emitSkip, emitRM,emitRestore
*/
int emitSkip( int howMany)
{ int i = emitLoc;
emitLoc += howMany ;
if (highEmitLoc < emitLoc) highEmitLoc = emitLoc ;
return i;
} /* emitSkip */
/* Procedure emitBackup backs up to
* loc = a previously skipped location
*/
void emitBackup( int loc)
{
if (loc > highEmitLoc) emitComment("BUG in emitBackup");
emitLoc = loc ;
} /* emitBackup */
/* Procedure emitRestore restores the current
* code position to the highest previously
* unemitted position
*/
void emitRestore(void)
{ emitLoc = highEmitLoc;}
/* Procedure emitRM_Abs converts an absolute reference
* to a pc-relative reference when emitting a
* register-to-memory TM instruction
* op = the opcode
* r = target register
* a = the absolute location in memory
* c = a comment to be printed if TraceCode is TRUE
*/
void emitRM_Abs( char *op, int r, int a, char * c)
{ fprintf(code,"%3d: %5s %d,%d(%d) ",
emitLoc,op,r,a-(emitLoc+1),pc);
++emitLoc ;
fprintf(code,"\t%s",c) ;
fprintf(code,"\n") ;
if (highEmitLoc < emitLoc) highEmitLoc = emitLoc ;
} /* emitRM_Abs */
// generate a lab
char* genLab()
{
sprintf(labelTable[labNum], "lab%d",labNum);
return labelTable[labNum++];
}