-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompiler.ts
More file actions
358 lines (345 loc) · 8.37 KB
/
compiler.ts
File metadata and controls
358 lines (345 loc) · 8.37 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
namespace OSECPU{
export const ROM_SIZE = 64;
}
var operand = function(type, byteIndex, bitOfs, binList, token){
var v;
switch(type){
case "R":
v = getRegNum(token, "R");
break;
case "P":
v = getRegNum(token, "P");
break;
case "L":
v = getLabelNum(token);
break;
case "T":
v = getTypeNum(token);
break;
case "i16":
v = parseInt16(token);
break;
case "i32":
v = parseInt32(token);
break;
case "i24":
v = parseInt24(token);
break;
default:
throw "Unknown operand type: " + type
}
if(binList[byteIndex] === undefined) binList[byteIndex] = 0;
binList[byteIndex] |= v << bitOfs;
return binList;
}
var op = function(type, byteIndex, bitOfs){
return function(binList, token){
return operand(type, byteIndex, bitOfs, binList, token);
}
}
var OSECPU_FPGA_INSTR_TYPE = {
"OPONLY": [],
"LBSET": [op("T", 0, 18), op("L", 0, 0), op("i16", 1, 16), op("i16", 1, 0)],
"LIMM16": [op("R", 0, 18), op("i16", 0, 0)],
"PLIMM": [op("P", 0, 18), op("L", 0, 0)],
"CND": [op("R", 0, 18)],
"1R1PT": [op("R", 0, 18), op("P", 0, 12), op("T", 0, 0)],
"2PT": [op("P", 0, 18), op("P", 0, 12), op("T", 0, 0)],
"1R2PT": [op("R", 0, 18), op("P", 0, 12), op("P", 0, 6), op("T", 0, 0)],
"3R": [op("R", 0, 18), op("R", 0, 12), op("R", 0, 6)],
"PCP": [op("P", 0, 12), op("P", 0, 6)],
"1R2P": [op("R", 0, 18), op("P", 0, 12), op("P", 0, 6)],
"LIMM32": [op("R", 0, 18), op("i32", 1, 0)],
"CP": [op("R", 0, 18), op("R", 0, 12)],
"CPDR": [op("R", 0, 12)],
"IMM24": [op("i24", 0, 0)],
}
var OSECPU_FPGA_INSTR_SET = {
"NOP": [0x00, "OPONLY"],
"LBSET": [0x01, "LBSET"],
"LIMM16": [0x02, "LIMM16"],
"PLIMM": [0x03, "PLIMM"],
"CND": [0x04, "CND"],
"LMEM": [0x08, "1R1PT"],
"SMEM": [0x09, "1R1PT"],
"PLMEM": [0x0A, "2PT"],
"PSMEM": [0x0B, "2PT"],
"PADD": [0x0E, "1R2PT"],
"PDIF": [0x0F, "1R2PT"],
"OR": [0x10, "3R"],
"XOR": [0x11, "3R"],
"AND": [0x12, "3R"],
"ADD": [0x14, "3R"],
"SUB": [0x15, "3R"],
"MUL": [0x16, "3R"],
"SHL": [0x18, "3R"],
"SAR": [0x19, "3R"],
"DIV": [0x1A, "3R"],
"MOD": [0x1B, "3R"],
"PCP": [0x1E, "PCP"],
"CMPE": [0x20, "3R"],
"CMPNE": [0x21, "3R"],
"CMPL": [0x22, "3R"],
"CMPGE": [0x23, "3R"],
"CMPLE": [0x24, "3R"],
"CMPG": [0x25, "3R"],
"TSTZ": [0x26, "3R"],
"TSTNZ": [0x27, "3R"],
"PCMPE": [0x28, "1R2P"],
"PCMPNE": [0x29, "1R2P"],
"PCMPL": [0x2A, "1R2P"],
"PCMPGE": [0x2B, "1R2P"],
"PCMPLE": [0x2C, "1R2P"],
"PCMPG": [0x2D, "1R2P"],
"LIMM32": [0xD0, "LIMM32"],
"LMEMCONV": [0xD1, "1R1PT"],
"CP": [0xD2, "CP"],
"CPDR": [0xD3, "CPDR"],
"END": [0xF0, "OPONLY"],
}
var OSECPU_FPGA_INSTR_MAP: any = {};
for(var k in OSECPU_FPGA_INSTR_SET){
var instrDef = OSECPU_FPGA_INSTR_SET[k];
var instrOperands = OSECPU_FPGA_INSTR_TYPE[instrDef[1]];
if(instrOperands === undefined){
console.error(
"not found in INSTR_TYPE " + instrDef[1]);
process.exit(1);
}
OSECPU_FPGA_INSTR_MAP[k] =
[instrDef[0]].concat(instrOperands);
}
class Instr
{
private opeRule;
private bin: number[];
constructor(opTable: any, tokenList: string[], index: number)
{
var mn = tokenList[index];
this.opeRule = opTable[mn];
if(this.opeRule === undefined){
throw "Unknown mnemonic: " + mn;
}
this.bin = [0];
for(var k = 1; k < this.opeRule.length; k++){
var token = tokenList[index + k];
this.bin = this.opeRule[k](this.bin, tokenList[index + k]);
}
this.bin[0] |= this.opeRule[0] << 24;
console.error("mn: " + mn);
}
getTokenCount(): number{
return this.opeRule.length;
}
getBin(): number[]
{
return this.bin;
}
}
class CodeBlock
{
private LBID: number;
private codeList: number[] = [];
private type: string;
constructor(LBID?: number, type?: string){
this.LBID = LBID;
this.type = type;
}
appendCode(bin: number[]){
this.codeList.push(bin[0]);
if(bin[1] !== undefined) this.codeList.push(bin[1]);
}
getSize(): number{
return this.codeList.length;
}
getHexStr(): string{
var s = "";
for(var i = 0; i < this.codeList.length; i++){
s += this.toHexStr32(this.codeList[i]) + "\n";
}
return s;
}
getLBID(){
return this.LBID;
}
genLBSETInstr(opTable: any, ofs: number){
return new Instr(opTable, [
"LBSET",
this.type,
"L" + this.getLBID(),
ofs.toString(),
this.getLabelDataCount().toString(),
], 0);
}
private getLabelDataCount(){
if(this.type.toUpperCase() === "CODE"){
return 1;
} else{
return this.getSize();
}
}
private toHexStr32(v: number): string
{
return ("00000000" + (v >>> 0).toString(16)).substr(-8);
}
}
class Assembler
{
private tokenSeparatorList = [];
private opTable: any;
static const
constructor(tokenSeparatorList: string[], opTable: any){
this.tokenSeparatorList = tokenSeparatorList;
this.opTable = opTable;
}
tokenize(input: string): string[]
{
var tokens =
input.splitByArraySeparatorSeparatedLong(this.tokenSeparatorList)
tokens.removeAllObject(" ");
tokens.removeAllObject("\t");
for(;;){
var pL = tokens.indexOf("/*");
if(pL == -1) break;
var pR = tokens.indexOf("*/", pL);
if(pR == -1) throw "Expected */, but not found.";
tokens.splice(pL, pR - pL + 1);
}
tokens.removeAllObject("\n");
return tokens;
}
compile(input: string)
{
var s = "";
try{
var tokenList = this.tokenize(input);
var codeBlockList: CodeBlock[] = [];
var currentBlock = undefined;
for(var i = 0; i < tokenList.length; ){
var mn = tokenList[i];
if(mn.substr(0, 2) === "LB"){
// new block
var lbNum = parseInt(tokenList[i + 1]);
getTypeNum(tokenList[i + 2]); // check
if(isNaN(lbNum)){
throw "Expected lbNum, but " + tokenList[i + 1];
}
currentBlock = new CodeBlock(lbNum, tokenList[i + 2]);
codeBlockList.push(currentBlock);
i += 3;
} else{
if(currentBlock === undefined){
throw "You need define label before code body";
}
var instr = new Instr(this.opTable, tokenList, i);
var bin = instr.getBin();
currentBlock.appendCode(bin);
i += instr.getTokenCount();
}
}
console.error(codeBlockList);
// generate LBSET instr
var binStr = "";
var header = new CodeBlock();
var ofs = codeBlockList.length * 2;
for(var i = 0; i < codeBlockList.length; i ++){
var block = codeBlockList[i];
var instr = block.genLBSETInstr(this.opTable, ofs);
var bin = instr.getBin();
header.appendCode(bin);
ofs += block.getSize();
}
codeBlockList.unshift(header);
// Generate HEX file
for(var i = 0; i < codeBlockList.length; i ++){
s += codeBlockList[i].getHexStr();
}
for(var i = ofs; i < OSECPU.ROM_SIZE; i++){
s += "00000000\n";
}
//
process.stdout.write(s);
} catch(e){
console.error(e);
process.exit(1);
}
}
}
function getLabelNum(token)
{
if(token[0] != "L") throw "Expected label identifier, but " + token[0];
var lbnum = parseInt(token.substr(1));
if(lbnum < 0 || 0x1000 <= lbnum){
throw "Out of bounds for label number (0-4095)"
}
return lbnum;
}
function getRegNum(token, type)
{
if(token[0] != type) throw "Expected register type is " + type + ", but " + token[0];
return parseInt(token.substr(1), 16);
}
function getTypeNum(token)
{
token = token.toUpperCase();
switch(token){
case "UNDEFINED": return 0x00;
case "VPTR": return 0x01;
case "SINT8": return 0x02;
case "UINT8": return 0x03;
case "SINT16": return 0x04;
case "UINT16": return 0x05;
case "SINT32": return 0x06;
case "UINT32": return 0x07;
case "SINT4": return 0x08;
case "UINT4": return 0x09;
case "SINT2": return 0x0A;
case "UINT2": return 0x0B;
case "SINT1": return 0x0C;
case "UINT1": return 0x0D;
case "CODE": return 0x3F;
}
throw "Unexpected label type: " + token;
}
function shiftedOperand(v, index){
// index: 0, 1, 2, 3
return v << (18 - index * 6);
}
function shiftedOpecode(op){
return op << 24;
}
function parseInt16(token: string)
{
var num = parseInt(token);
if(num < 0){
num = num & 0xffff;
}
return num;
}
function parseInt24(token: string)
{
var num = parseInt(token);
if(num < 0){
num = num & 0xffffff;
}
return num;
}
function parseInt32(token: string)
{
return parseInt(token);
}
if(process.argv.length < 3){
console.log("node compiler.js <source>");
process.exit(0);
}
var fs = require('fs');
fs.readFile(process.argv[2], 'utf8', function (err, text) {
if(err){
console.log('File open failed. ' + err);
process.exit(1);
}
var compiler = new Assembler([
" ", "\t", "\n", "/*", "*/"
], OSECPU_FPGA_INSTR_MAP);
compiler.compile(text);
});