Repository navigation
Expand file tree
/
Copy pathemitcode.cpp
More file actions
233 lines (180 loc) · 5.18 KB
/
Copy pathemitcode.cpp
File metadata and controls
233 lines (180 loc) · 5.18 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// TM Code emitting utilities
// implementation for the TINY compiler
// Compiler Construction: Principles and Practice
// Kenneth C. Louden
// Modified slightly by Jorge L. Williams
// Modified Nov 13, 2020 Robert Heckendorn
//
// The two comment string forms of the calls allow you to easily
// compose a comment from text and a symbol name for example.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "emitcode.h"
extern FILE *code;
// TM location number for current instruction emission
static int emitLoc = 0; // next empty slot in Imem growing to lower memory
static int litLoc = 1; // next empty slot in Dmem growing to higher memory
// Procedure emitComment prints a comment line
// with a comment that is the concatenation of c and d
//
void emitComment(char *c, char *cc)
{
fprintf(code, "* %s %s\n", c, cc);
}
void emitComment(char *c, int n)
{
fprintf(code, "* %s %d\n", c, n);
}
// Procedure emitComment prints a comment line
// with comment c in the code file
//
void emitComment(char *c)
{
fprintf(code, "* %s\n", c);
}
// emitRO emits a REGISTER-ONLY TM instruction
// op = the opcode
// r = target register
// s = 1st source register
// t = 2nd source register
// c = a comment
//
void emitRO(char *op, long long int r, long long int s, long long int t, char *c, char *cc)
{
fprintf(code, "%3d: %5s %lld,%lld,%lld\t%s %s\n", emitLoc, op, r, s, t, c, cc);
fflush(code);
emitLoc++;
}
void emitRO(char *op,long long int r,long long int s,long long int t, char *c)
{
emitRO(op, r, s, t, c, (char *)"");
}
// emitRM emits a REGISTER-TO-MEMORY TM instruction
// op = the opcode
// r = target register
// d = the offset
// s = the base register
// c = a comment
//
void emitRM(char *op, long long int r, long long int d, long long int s, char *c, char *cc)
{
fprintf(code, "%3d: %5s %lld,%lld(%lld)\t%s %s\n", emitLoc, op, r, d, s, c, cc);
fflush(code);
emitLoc++;
}
void emitRM(char *op,long long int r,long long int d,long long int s, char *c)
{
emitRM(op, r, d, s, c, (char *)"");
}
void emitGoto(int d,long long int s, char *c, char *cc)
{
emitRM((char *)"JMP", (long long int)PC, d, s, c, cc);
}
void emitGoto(int d,long long int s, char *c)
{
emitGoto(d, s, c, (char *)"");
}
// emitRMAbs 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
//
void emitRMAbs(char *op, long long int r, long long int a, char *c, char *cc)
{
fprintf(code, "%3d: %5s %lld,%lld(%lld)\t%s %s\n", emitLoc, op, r, a - (long long int)(emitLoc + 1),
(long long int)PC, c, cc);
fflush(code);
emitLoc++;
}
void emitRMAbs(char *op,long long int r,long long int a, char *c)
{
emitRMAbs(op, r, a, c, (char *)"");
}
void emitGotoAbs(int a, char *c, char *cc)
{
emitRMAbs((char *)"JMP", (long long int)PC, a, c, cc);
}
void emitGotoAbs(int a, char *c)
{
emitGotoAbs(a, c, (char *)"");
}
// emit a string literal instruction
//
// IMPORTANT: assumes litterals are stored in the global space at an
// address which is an offset from the beginning of global space.
//
// The input is a NEGATIVE offset from R0 and the output LIT instruction takes
// a POSITIVE distance from R0.
//
// The string is stored from higher order address to lower order address!!
// An example assuming R0=9999:
//
// 2: LIT "horse"
//
// 9999 blah
// 9998 5
// 9997 h <-- location of string
// 9996 o
// 9995 r
// 9994 s
// 9993 e
// 9992 blah
// 9991 blah
// 9990 blah
int emitStrLit(int goffset, char *s)
{
fprintf(code, "%3d: %5s \"%s\"\n", -goffset, (char *)"LIT", s);
return goffset;
}
//
// Backpatching Functions
//
// asks where the next instruction will go. Same as emitSkip(0).
int emitWhereAmI()
{
return emitLoc;
}
// emitSkip skips "howMany" code
// locations for later backpatch.
// It also returns the current code position.
// emitSkip(0) tells you where you are and reserves no space.
//
int emitSkip(int howMany)
{
int i = emitLoc;
emitLoc += howMany;
return i;
}
// emitNewLoc sets the emitLoc to a new location
// often loc = a previously skipped location
//
void emitNewLoc(int loc)
{
emitLoc = loc;
}
// this back patches a LDA at the instruction address addr that
// jumps to the current instruction location now that it is known.
// This is essentially a backpatched "goto"
void backPatchAJumpToHere(int addr, char *comment)
{
int currloc;
currloc = emitWhereAmI(); // remember where we are
emitNewLoc(addr); // go to addr
emitGotoAbs(currloc, comment); // the LDA to here
emitNewLoc(currloc); // restore addr
}
// this back patches a JZR or JNZ at the instruction address addr that
// jumps to the current instruction location now that it is known.
void backPatchAJumpToHere(char *cmd, int reg, int addr, char *comment)
{
int currloc;
currloc = emitWhereAmI(); // remember where we are
emitNewLoc(addr); // go to addr
emitRMAbs(cmd, reg, currloc, comment); // cmd = JZR, JNZ
emitNewLoc(currloc); // restore addr
}