-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotepad
More file actions
572 lines (305 loc) · 7.68 KB
/
notepad
File metadata and controls
572 lines (305 loc) · 7.68 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
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
sss
struct {
number
array[10], new lines id 2 items per row
string
num
} struct don't print no new lines and no field names
12, [ 1, 2, 3, 4.. ], str, 32
12, [ 0 = 1, 1 = 2, 3, 4.. ], str, 32
12, [
0 = 1,
1 = 2,
3,
4..
], str, 32
12, [
0 = 1, 1 = 2,
3, 4..
], str, 32
[
[ { 0, 248, 0, 0 }, { 224, 7, 0, 0 } ],
[ { 31, 0, 0, 0 } { 0, 0, 0, 0 } ],
[ { 0, 0, 0, 0 } { 0, 0, 0, 0 } ],
[ { 0, 0, 0, 0 } { 0, 0, 0, 0 } ],
[ { 0, 0, 0, 0 } { 0, 0, 0, 0 } ],
[ { 0, 0, 0, 0 } { 0, 0, 0, 0 } ],
[ { 0, 0, 0, 0 } { 0, 0, 0, 0 } ],
[ { 0, 0, 0, 0 } { 0, 0, 0, 0 } ],
[ { 0, 0, 0, 0 } { 0, 0, 0, 0 } ],
[ { 0, 0, 0, 0 } { 0, 0, 0, 0 } ]
]
available = 8
read_byte
u8 lower_8_bits_mask = (1 << 8) - 1
00000000_00000000_00000000_00000000_00000000_00000000_00000000_11111111
u8 = (bits & lower_8_bits_mask);
stream: 01001000_0010010_1010100_00111110
^
bit_header -> 4
bits: 00000000_00000000_00000000_00000000_00000000_00000000_00000000_00000100
^
read 6 bits -> grow to next biggest multiple of 8 -> read 8 bits
bit_header = 4 + 8 => 12 (read 3 bits)
00000000_00000000_00000000_00000000_00000000_00000000_00000000_00000100
bit_masks = ( ( 1 << bits ) - 1) << bit_header
)
6 => 8
4 => 8
1..7 => 8
9..15 => 16
available = 8
read_byte
u8 lower_8_bits_mask = (1 << 8) - 1
00000000_00000000_00000000_00000000_00000000_00000000_00000000_11111111
u8 = (bits & lower_8_bits_mask);
read_
=================================================
bit_header = 1 (we are at bit 1)
easy one if bits rewind bits + bit_header < 64
00000000_00000000_00000000_00000000_00000000_00000000_00000000_00000100
^
rewind 32
00000000_00000000_00000000_00000000_00000000_00000000_00000000_00000100
^
at bit 33 now
--
bit_header = 32
easy one if bits rewind bits + bit_header < 64
00000000_00000000_00000000_00000000_00000000_00000000_00000000_00000100
^ ^
rewind 32
00000000_00000000_00000000_00000000_00000000_00000000_00000000_00000100
^
at 64
--
bit_header = 64
00000000_00000000_00000000_00000000_00000000_00000000_00000000_00000100
^
rewind 32
stream -= 4 (move 4 bytes back)
shift bits >> 32 bits
00000000_00000000_00000000_00000000_00000000_00000000_00000000_00000100
^
read 32 bits << bit_header | bits
at 64
-----
bit_header = 42
00000000_00000000_000000|00_00000000_00000000_00000000_00000000_00000100
rewind 32
42 + 32 => 74 > 64 -> need_to_read back
74 - 64 => 10 bits to read back -> 2 bytes upped, we have 32 - 10 => 22 bits already
16 - 10 => 8 bits dropped
00000000_00000000_000000|00_00000000_00000000_00000000_00000000_00000100
>> 16 down
42 - 16 => 26 bit_head
00000000_00000000_00000000_00000000_000000|00_00000000_00000000_00000100
///////////////////////
var_decl => read imm
if init_expr => operator => delay
expr statements => eval_expr
///////////////////////////////////////////////////////////////////////////////////////////
move
mem
reg
mem => scale + index + base + disp32
if mem+sib
modrm => jk
mov
left - stack, register, memory
right - satck, register, memory
stack reg mem imm?
left
stack
reg
mem
imm?
ops reduction:
add (no three op, result)
left op
right op
always result = left + right
left = left + right
(immediate? cannot be immediate, but we load all immediates into register?)
maybe this is wrong, save one move for immediate value?
- should add op be responsible for the store back to location?
currently it is
left - stack, register, memory
right - satck, register, memory
stack reg mem imm?
left
stack mov left to reg first
add from stack
(mov reg, stack)
( add reg, stack )
reg add stack to reg
mem mov mem to reg
imm?
both stack and mem are just mem movs, difference is SIB and ModRM
mov reg, mem, stack - diff only in ModRM and SIB?
- if we choose mov for left op be
i32 foo = 1;
mov foo, 1
foo = foo + 2 => add foo, 2
i32 foo = (1 + 5*2) + 2;
could be this:
mov foo, 1
add foo, 2
compared to this
mov reg, foo
add reg, 2
mov foo, reg
--
i32 foo = (1 + 5*2) + 2;
this
mov foo, 1
mov reg, 5
mul reg, 2
add foo, reg
add foo, 2
compared to
mov reg1, 1
mov reg2, 5
mul reg2, 2
add reg1, reg2
mov reg2, 2
add reg1, reg2
mov foo, reg1
--
i32 foo = 5;
foo = foo*foo + foo;
mov reg, foo
mul reg, reg
add reg, foo
mov foo, reg
// can we recursively know that a value of the stack has been loaded into a register?
============================================================
tomorrow todo:
-- calls with params -> stack
- store some vars (rcx, rdx, r8, r9)
-- local variables -> assigned on stack
if cond = cmp 5, 2
cmp eax, 1?
jneq
{
next_label <- // jump next if or else?
..do if .. body
..jmp end...
} // next label pos = size of generated body?
next_lbl: elif: ( 1 > 2 )
jneq
{
next_label <- // jump next if or else?
..do if .. body
..jmp end...
}
next_lbl: else:
..else body..
end -- end of stmt
.....
===============================================================
x64 calling convention
- RAX, RCX, RDX, R8, R9, R10, R10 => can be overwritten, no need to save ?
- calle should not worry about these
- caller should -> save restore
- RBX, RBP, RSP, RDI, RSI, R12-15 => must be saved in the func called later restored?
// /
RAX <= RAX
save value push RAX
1 + func();
[ RAX ]
1 + 2
12+
push 1
push 2
push +
pop +
val = 1 + 2
push val
1
left => mov RAX <=1
push RAX
right <= mov RAX <= 2
RAX < 2
ADD RAX + 1
add RAX, RAX => 4
pop RAX
==========================================
type -> defined_type
struct -> type -> decls -> var_decls -> types too
struct can be a type
fields
var decls; *
typedef that creates an alias
decl | type -> type name
aliases another type
typedef void (*func_type)(void);
func_type *ptr = { func1, func2, func3 };
+
1
ptr
func_type*
return type is func_type*
*
ptr
return func_type base
(*ptr)();
============================================================
todo
- arrary index && array init/compound literals done
- array init/literal - done
i32[] meow = [1, 2, 3, 4] <> { 1, 2, 3, 4}
mewo[0] = 32;
- field access -> or . => done
struct x {
i32 meow;
}
x t = { 2 } // comp_lit
t.meow = 4;
x *some = &t;
some->meow = 5;
- address of operator - done
i32 some_var = 2;
i32 *ptr = nil;
ptr = &some_var;
- ptr deref - done
i32 i = *ptr;
- switch cases
switch(condi) { switch_body }
- else & else if:
parse and type check
============================================================
code:
setting up static data must be done before jumpping into the code
so before code run:
- mv str "hello world" to data_section
- maybe cpy to mem?
setup stack ptr
===========================================================
4 bytes for i; on the stack?
4 bytes for x
4 bytes for y;
first temp mem:
write 1 to data <- addr on decl i
write 2 to data <- addr on decl x
mov i -> rax
mov x -> rbx
add rax, rbx
mov rax -> @y_mem
write
=========================================================
==========================================================
Code Gen Setup:
- move RSP => RAX
- MOVE RBP => RCX
- SET RBP & RSP to your own memory locations
- PUSH RAX, RCX => save stack ptr and base
- END Of "main" func =>
- pop stack top => RSP, RBP restore the stack
go through decls =>
if id == main => assign main_memory to rand_func
jump
- jump into "main_function"
generating functions:
- they are just memory addresses to jump to
- every function must save some registers and restore them if used