-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.cpp
205 lines (190 loc) · 4.05 KB
/
generator.cpp
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
#include "generator.h"
#include <vector>
#include <stack>
#include <stdio.h>
#include <stdint.h>
struct TPendingInstr
{
uint16_t offset;
uint16_t opcode;
uint32_t labelAddr;
uint8_t addrStart, addrLen;
uint8_t addrRelative;
};
struct TLabel
{
string name;
uint16_t addr;
};
FILE *of;
int addr;
vector<TPendingInstr> pending;
vector<TLabel> labels;
stack<FILE*> files;
stack<int> lineno;
stack<string> filenames;
bool passNL = false;
string genGetFilename ()
{
return filenames.top ();
}
int genGetAddr ()
{
return addr;
}
void genSetLabel (const string& label)
{
for (int i = 0; i < labels.size (); i++)
{
if (labels[i].name == label)
{
labels[i].addr = genGetAddr ();
return;
}
}
TLabel lab;
lab.name = label;
lab.addr = genGetAddr ();
labels.push_back (lab);
}
int genGetLabelId (const string& label)
{
for (int i = 0; i < labels.size (); i++)
if (labels[i].name == label)
return i;
TLabel lab;
lab.name = label;
lab.addr = 0;
labels.push_back (lab);
return labels.size () - 1;
}
void genInit (const char* infile, const char* outfile)
{
FILE* inf = fopen (infile, "rb");
of = fopen (outfile, "wb");
addr = 0;
yylineno = 1;
files.push (inf);
filenames.push (infile);
}
void genClose ()
{
genWritePending ();
fclose (of);
}
void genIncludeFile (string path)
{
FILE *f = fopen (path.c_str (), "rb");
files.push (f);
passNL = true;
lineno.push (yylineno);
yylineno = 0;
filenames.push (path);
}
int genGetChar (char *buf)
{
if (passNL)
{
buf[0] = '\n';
passNL = false;
return 1;
}
int c = fgetc (files.top ());
if (c == EOF)
{
if (files.size () > 1)
{
fclose (files.top ());
files.pop ();
yylineno = lineno.top ();
lineno.pop ();
filenames.pop ();
c = fgetc (files.top ());
}
}
return (c == EOF) ? 0 : (buf[0] = c, 1);
}
void genWritePending ()
{
for (int i = 0; i < pending.size (); i++)
{
uint16_t base = pending[i].offset;
uint16_t opcode = pending[i].opcode;
printf ("base: %02x opcode %04x la: %08x\n", base, opcode, pending[i].labelAddr);
uint16_t addr;
if ((pending[i].labelAddr & 0x80000000) && !(pending[i].labelAddr & 0x40000000))
{
addr = labels[pending[i].labelAddr & ~0x80000000].addr;
printf ("label addr: %04x\n", addr);
opcode |= genGetAddrOffset (addr, base, pending[i].addrLen) << pending[i].addrStart;
}
else
{
addr = pending[i].labelAddr;
printf ("label r addr: %04x\n", addr);
opcode |= genGetAddrOffset (addr, base, pending[i].addrLen) << pending[i].addrStart;
printf ("label r opcode: %04x\n", opcode);
}
if (pending[i].addrLen >= 16)
{
outputInstr16offset (base, pending[i].opcode);
outputInstr16offset (base + 2, genGetAddrOffset (addr+2, 0, pending[i].addrLen));
}
else
{
printf ("QWEWE\n");
outputInstr16offset (base, opcode);
}
}
}
int genGetAddrOffset (int dest, int base, int bits)
{
return (((dest-base)/2-1) & ((1 << bits) - 1));
}
void outputInstr16 (uint16_t val)
{
fwrite ((uint8_t*)&val, 2, 1, of);
addr += 2;
}
void outputInstr16offset (uint16_t offset, uint16_t val)
{
fseek (of, offset, SEEK_SET);
fwrite ((uint8_t*)&val, 2, 1, of);
}
void genAppendPending (uint16_t opcode, uint32_t addr, uint8_t addrStart, uint8_t addrLen)
{
TPendingInstr pend;
pend.offset = genGetAddr ();
pend.opcode = opcode;
pend.addrStart = addrStart;
pend.addrLen = addrLen;
pend.addrRelative = 1;
pend.labelAddr = addr;
pending.push_back (pend);
outputInstr16 (0xffff);
}
void genAppendPending32 (uint16_t opcode, uint32_t addr, uint8_t addrStart, uint8_t addrLen)
{
TPendingInstr pend;
pend.offset = genGetAddr ();
pend.opcode = opcode;
pend.addrStart = addrStart;
pend.addrLen = addrLen;
pend.addrRelative = 0;
pend.labelAddr = addr;
pending.push_back (pend);
outputInstr16 (0xffff);
outputInstr16 (0xffff);
}
uint16_t packRdRr (uint8_t Rd, uint8_t Rr)
{
return (Rd << 4) | ((Rr & 0x10) << 5) | (Rr & 0x0f);
}
uint16_t packRd8K16 (uint8_t Rd, uint8_t K)
{
return ((Rd-16) << 4) | ((K & 0xf0) << 4) | (K & 0x0f);
}
uint16_t packRr6q (uint8_t Rr, uint8_t q)
{
return (Rr << 4) | ((q & 0x20) << 8) | ((q & 0x18) << 7) | (q & 0x07);
}