-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBytecode.neon
515 lines (451 loc) · 15 KB
/
Bytecode.neon
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
%|
| File: Bytecode
|
| Functions for creating, reading and writing Neon bytecode.
|%
IMPORT file
IMPORT hash
IMPORT struct
%|
| Exception: BytecodeException
|
| Indicates that the selected byte stream is either corrupt, or not a valid Neon bytecode stream.
|%
EXPORT DECLARE EXCEPTION BytecodeException
%|
| Type: CodeBytes
|
| A stream of bytes that comprise a valid Neon bytecode stream.
|%
EXPORT TYPE CodeBytes IS Array<Number>
%|
| Type: Type
|
| A RECORD defined in the bytecode.
|
| Fields:
| name - the string table index of the name of this RECORD type.
| descriptor -
|%
EXPORT TYPE Type IS RECORD
name : Number
descriptor : Number
END RECORD
%|
| Type: Function
|
| A function that is exported from the neon bytecode stream.
|
| Fields:
| name - the string table index of the name of this FUNCTION type.
| descriptor -
| entry - bytecode offset to the entry point of this function.
|%
EXPORT TYPE Function IS RECORD
name : Number
descriptor : Number
entry : Number
END RECORD
%|
| Type: Constant
|
| A constant that is exported from the neon bytecode stream.
|
| Fields:
| name - the string table index of the name of this CONSTANT.
| type -
| value - array of numbers that represent the value of this constant.
|%
EXPORT TYPE Constant IS RECORD
name : Number
type : Number
value : Array<Number>
END RECORD
%|
| Type: Variable
|
| A variable that is exported from the neon bytecode stream.
|
| Fields:
| name - the string table index of the name of this variable.
| type -
| index -
|%
EXPORT TYPE Variable IS RECORD
name : Number
type : Number
index : Number
END RECORD
%|
| Type: ExceptionExport
|
| An exception that is exported from the neon bytecode stream.
|
| Fields:
| name - the string table index of the name of this EXCEPTION.
|%
EXPORT TYPE ExceptionExport IS RECORD
name : Number
END RECORD
%|
| Type: ExceptInfo
|
| A function that is exported from the neon bytecode stream.
|
| Fields:
| name - the string table index of the name of this FUNCTION type.
| descriptor -
| entry - bytecode offset to the entry point of this function.
|%
EXPORT TYPE ExceptInfo IS RECORD
start : Number
end : Number
excid : Number
handler : Number
END RECORD
%|
| Type: FunctionInfo
|
| A stream of bytes that comprise a valid Neon bytecode stream.
|%
EXPORT TYPE FunctionInfo IS RECORD
name : Number
entry : Number
END RECORD
%|
| Type: Pair
|
| A stream of bytes that comprise a valid Neon bytecode stream.
|%
EXPORT TYPE Pair IS RECORD
first : Number
second : String
END RECORD
%|
| Type: Bytecode
|
| Fields:
| obj - the actual object code.
| source_hash - the hash of the <code> bytes.
| global_size - the number of global variables requested.
| strtable - the static data table for both strings, and numbers.
| export_types - the defined Neon data structures being exported from the bytecode, if any.
| export_constants - the defined constants being exported by the bytecode, if any.
| export_variables - the defined variables being exported by the bytecode, if any.
| export_functions - the defined functions being exported by the bytecode, if any.
| export_exceptions - the defined exceptions being exported by the bytecode, if any.
| imports - the requested imports required by the bytecode, if any.
| functions - an array of FunctionInfo for each function in the Neon bytecode, if any.
| exceptions - an array of ExceptInfo for each exception, and the handler for that Structured Exception Handler block.
| code - the Neon bytecode stream.
|%
EXPORT TYPE Bytecode IS RECORD
obj: CodeBytes
source_hash: Array<Number>
global_size: Number
strtable: Array<String>
export_types: Array<Type>
export_constants: Array<Constant>
export_variables: Array<Variable>
export_functions: Array<Function>
export_exceptions: Array<ExceptionExport>
imports: Array<Pair>
functions: Array<FunctionInfo>
exceptions: Array<ExceptInfo>
code: CodeBytes
END RECORD
%|
| Function: reset
|
| Resets the Bytecode object, leaving it ready to build a new Bytecode object.
|
| See Also:
| <fromBytes>
| <load>
| <getBytes>
| <toFile>
|
| Example:
| > VAR code: Bytecode := Bytecode()
| > Bytecode.reset()
|%
FUNCTION Bytecode.reset(INOUT self: Bytecode)
LET new: Bytecode := Bytecode(global_size WITH 1)
self := new
END FUNCTION
FUNCTION Bytecode.fromBytes(INOUT self: Bytecode, bytes: Bytes): Bytecode
IF self.load(bytes.toArray()) THEN
RETURN self
END IF
RETURN self
END FUNCTION
FUNCTION Bytecode.load(INOUT self: Bytecode, bytes: Array<Number>): Boolean
self.obj := bytes
VAR i: Number := 0
IF self.obj.size() < 32 THEN
RAISE BytecodeException(info WITH "Invalid Neon Bytecode file.")
END IF
FOR x := 0 TO 31 DO
self.source_hash[x] := self.obj[i+x]
END FOR
i := i + 32
self.global_size := get_uint16(self.obj[i TO i+1], INOUT i)
LET strtablesize: Number := get_uint32(self.obj[i TO i+3], INOUT i)
IF (i+strtablesize > self.obj.size()) THEN
RAISE BytecodeException(info WITH "Corrupted String Table in .neonx file.")
END IF
self.strtable := getstrtable(i, i + strtablesize, self.obj)
i := i + strtablesize
VAR typesize: Number := get_uint16(self.obj[i TO i+1], INOUT i)
WHILE typesize > 0 DO
VAR t: Type := Type()
t.name := get_uint16(self.obj[i TO i+1], INOUT i)
t.descriptor := get_uint16(self.obj[i TO i+1], INOUT i)
self.export_types.append(t)
DEC typesize
END WHILE
VAR constantsize: Number := get_uint16(self.obj[i TO i+1], INOUT i)
WHILE constantsize > 0 DO
VAR c: Constant := Constant()
c.name := get_uint16(self.obj[i TO i+1], INOUT i)
c.type := get_uint16(self.obj[i TO i+1], INOUT i)
LET size: Number := get_uint16(self.obj[i TO i+1], INOUT i)
IF (i+size > self.obj.size()) THEN
RAISE BytecodeException(info WITH "Corrupted constants in .neonx file.")
END IF
c.value := self.obj[i TO i+size]
i := i + size
self.export_constants.append(c)
DEC constantsize
END WHILE
VAR variablesize: Number := get_uint16(self.obj[i TO i+1], INOUT i)
WHILE variablesize > 0 DO
VAR v: Variable := Variable()
v.name := get_uint16(self.obj[i TO i+1], INOUT i)
v.type := get_uint16(self.obj[i TO i+1], INOUT i)
v.index := get_uint16(self.obj[i TO i+1], INOUT i)
self.export_variables.append(v)
DEC variablesize
END WHILE
VAR functionsize: Number := get_uint16(self.obj[i TO i+1], INOUT i)
WHILE functionsize > 0 DO
VAR f: Function := Function()
f.name := get_uint16(self.obj[i TO i+1], INOUT i)
f.descriptor := get_uint16(self.obj[i TO i+1], INOUT i)
f.entry := get_uint16(self.obj[i TO i+1], INOUT i)
self.export_functions.append(f)
DEC functionsize
END WHILE
VAR exceptionexportsize: Number := get_uint16(self.obj[i TO i+1], INOUT i)
WHILE exceptionexportsize > 0 DO
VAR e: ExceptionExport := ExceptionExport()
e.name := get_uint16(self.obj[i TO i+1], INOUT i)
self.export_exceptions.append(e)
DEC exceptionexportsize
END WHILE
VAR importsize: Number := get_uint16(self.obj[i TO i+1], INOUT i)
WHILE importsize > 0 DO
VAR imp: Pair := Pair()
imp.first := get_uint16(self.obj[i TO i+1], INOUT i)
IF (i+32 > self.obj.size()) THEN
RAISE BytecodeException(info WITH "Corrupted Import Table in neon excutable file.")
END IF
FOREACH s IN self.obj[i TO i+32] DO
imp.second.append(str(s))
END FOREACH
print("imp.second=\(imp.second)")
i := i + 32
self.imports.append(imp)
DEC importsize
END WHILE
functionsize := get_uint16(self.obj[i TO i+1], INOUT i)
WHILE functionsize > 0 DO
VAR f: FunctionInfo := FunctionInfo()
f.name := get_uint16(self.obj[i TO i+1], INOUT i)
f.entry := get_uint16(self.obj[i TO i+1], INOUT i)
self.functions.append(f)
DEC functionsize
END WHILE
VAR exceptionsize: Number := get_uint16(self.obj[i TO i+1], INOUT i)
WHILE exceptionsize > 0 DO
VAR e: ExceptInfo := ExceptInfo()
e.start := get_uint16(self.obj[i TO i+1], INOUT i)
e.end := get_uint16(self.obj[i TO i+1], INOUT i)
e.excid := get_uint16(self.obj[i TO i+1], INOUT i)
e.handler := get_uint16(self.obj[i TO i+1], INOUT i)
self.exceptions.append(e)
DEC exceptionsize
END WHILE
self.code := self.obj[i TO LAST]
RETURN TRUE
END FUNCTION
FUNCTION Bytecode.getStringIndex(INOUT self: Bytecode, s: String): Number
FOREACH st IN self.strtable INDEX i DO
IF st = s THEN
RETURN i
END IF
END FOREACH
RETURN -1
END FUNCTION
FUNCTION Bytecode.addString(INOUT self: Bytecode, s: String): Number
FOREACH st IN self.strtable INDEX i DO
IF st = s THEN
RETURN i
END IF
END FOREACH
self.strtable.append(s)
RETURN self.strtable.size() - 1
END FUNCTION
FUNCTION Bytecode.addFunction(INOUT self: Bytecode, fi: FunctionInfo)
FOREACH f IN self.functions INDEX i DO
IF self.strtable[fi.name] = self.strtable[f.name] THEN
self.functions[i].entry := fi.entry
EXIT FUNCTION
END IF
END FOREACH
self.functions.append(fi)
END FUNCTION
FUNCTION Bytecode.referenceString(INOUT self: Bytecode, index: Number)
put_uint32(INOUT self.code, index)
END FUNCTION
FUNCTION Bytecode.referenceVariable(INOUT self: Bytecode, Value: Number)
put_uint32(INOUT self.code, Value)
END FUNCTION
FUNCTION Bytecode.referenceBoolean(INOUT self: Bytecode, Value: Boolean)
put_uint8(INOUT self.code, (IF Value THEN 1 ELSE 0))
END FUNCTION
FUNCTION Bytecode.referenceAddress(INOUT self: Bytecode, address: Number)
put_uint32(INOUT self.code, address)
END FUNCTION
FUNCTION Bytecode.fixupAddress(INOUT self: Bytecode, address: Number, value: Number)
set_uint32(INOUT self.code, address, value)
END FUNCTION
FUNCTION Bytecode.toFile(INOUT self: Bytecode, fn: String)
file.writeBytes(fn, self.getBytes())
END FUNCTION
FUNCTION Bytecode.getCurrentAddress(INOUT self: Bytecode): Number
RETURN self.code.size()
END FUNCTION
FUNCTION Bytecode.getBytes(INOUT self: Bytecode): Bytes
VAR obj: Array<Number> := []
IF self.source_hash.size() # 32 THEN
self.source_hash.extend(hash.sha256Raw(self.code.toBytes()).toArray())
END IF
obj.extend(self.source_hash)
put_uint16(INOUT obj, self.global_size)
VAR st: Array<Number> := []
FOREACH s IN self.strtable DO
put_uint32(INOUT st, s.length())
st.extend(s.toBytes().toArray())
END FOREACH
put_uint32(INOUT obj, st.size())
obj.extend(st)
put_uint16(INOUT obj, self.export_types.size())
FOREACH t IN self.export_types DO
put_uint16(INOUT obj, t.name)
put_uint16(INOUT obj, t.descriptor)
END FOREACH
put_uint16(INOUT obj, self.export_constants.size())
FOREACH c IN self.export_constants DO
put_uint16(INOUT obj, c.name)
put_uint16(INOUT obj, c.type)
put_uint16(INOUT obj, c.value.size())
obj.extend(c.value)
END FOREACH
put_uint16(INOUT obj, self.export_variables.size())
FOREACH v IN self.export_variables DO
put_uint16(INOUT obj, v.name)
put_uint16(INOUT obj, v.type)
put_uint16(INOUT obj, v.index)
END FOREACH
put_uint16(INOUT obj, self.export_functions.size())
FOREACH f IN self.export_functions DO
put_uint16(INOUT obj, f.name)
put_uint16(INOUT obj, f.descriptor)
put_uint16(INOUT obj, f.entry)
END FOREACH
put_uint16(INOUT obj, self.export_exceptions.size())
FOREACH e IN self.export_exceptions DO
put_uint16(INOUT obj, e.name)
END FOREACH
put_uint16(INOUT obj, self.imports.size())
FOREACH i IN self.imports DO
put_uint16(INOUT obj, i.first)
ASSERT i.second.length() = 32
obj.extend(i.second.toBytes().toArray())
END FOREACH
put_uint16(INOUT obj, self.functions.size())
FOREACH f IN self.functions DO
put_uint16(INOUT obj, f.name)
put_uint16(INOUT obj, f.entry)
END FOREACH
put_uint16(INOUT obj, self.exceptions.size())
FOREACH e IN self.exceptions DO
put_uint16(INOUT obj, e.start)
put_uint16(INOUT obj, e.end)
put_uint16(INOUT obj, e.excid)
put_uint16(INOUT obj, e.handler)
END FOREACH
obj.extend(self.code)
RETURN obj.toBytes()
END FUNCTION
FUNCTION put_uint8(INOUT obj: Array<Number>, x: Number)
obj.extend(struct.packUInt8(x).toArray())
END FUNCTION
FUNCTION put_uint16(INOUT obj: Array<Number>, x: Number)
obj.extend(struct.packUInt16BE(x).toArray())
END FUNCTION
FUNCTION put_uint32(INOUT obj: Array<Number>, x: Number)
obj.extend(struct.packUInt32BE(x).toArray())
END FUNCTION
FUNCTION set_uint32(INOUT obj: Array<Number>, addr: Number, value: Number)
obj[addr TO addr+3] := struct.packUInt32BE(value).toArray()
END FUNCTION
FUNCTION get_uint16(IN obj: Array<Number>, INOUT i: Number): Number
LET r: Number := struct.unpackUInt16BE(obj.toBytes())
i := i + 2
RETURN r
END FUNCTION
FUNCTION get_uint32(IN obj: Array<Number>, INOUT i: Number): Number
LET r: Number := struct.unpackUInt32BE(obj.toBytes())
i := i + 4
RETURN r
END FUNCTION
FUNCTION getstrtable(start, end: Number, obj: Array<Number>): Array<String>
VAR r: Array<String> := []
VAR s, e: Number := 0
s := start
e := end
WHILE s # e DO
LET len: Number := get_uint32(obj[s TO s+3], INOUT s)
VAR strent: String := ""
FOR x := s TO s+len-1 DO
strent.append(chr(obj[x]))
INC s
END FOR
r.append(strent)
END WHILE
RETURN r
END FUNCTION
BEGIN MAIN
IMPORT sys
VAR code: Bytecode := Bytecode()
code.global_size := 1
code.strtable.append("print")
code.strtable.append("Hello, World!")
code.code.extend([0x04, 0x00, 0x00, 0x00, 0x01, 0x3E, 0x00, 0x00, 0x00, 0x00, 0x49])
code.source_hash.extend(hash.sha256Raw(code.code.toBytes()).toArray())
file.writeBytes("./tmp/asmTest.neonx", code.getBytes())
%sys.exit(0)
TRY
VAR bytes: Bytes := file.readBytes("./tmp/asmTest.neonx")
code.reset()
IF code.load(bytes.toArray()) THEN
print("Loaded code successfully.")
ELSE
print("Failed to load code.")
END IF
EXCEPTION file.FileOpenException DO
print("Unable to open select neonx file.")
END TRY
END MAIN