-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathValue.c
590 lines (540 loc) · 18.7 KB
/
Value.c
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
#include "Value.h"
#include "Outfile.h"
#include "Register.h"
#include "Stack.h"
#include "Variables.h"
#include <stdbool.h>
int Value_GetR0(const Value* v)
{
assert(v->addressType == AddressType_Register || v->addressType == AddressType_MemoryRegister);
return ((uint16_t)v->address) % 8;
}
int Value_GetR1(const Value* v)
{
assert(v->addressType == AddressType_Register || v->addressType == AddressType_MemoryRegister);
assert(v->address != (int32_t)(-1));
if (v->size < 2)
return -1;
return ((uint16_t)v->address) / 8;
}
void Value_ValueAddrToStackPointer(const Value* v)
{
assert(v->addressType == AddressType_MemoryRelative);
Stack_ToAddress((int)v->address);
}
Value Value_FromRegister(int r0)
{
return (Value){(int32_t)r0, AddressType_Register, 1};
}
Value Value_FromRegisters(int r0, int r1)
{
return (Value){(int32_t)(r0 + r1 * 8), AddressType_Register, 2};
}
Value Value_Register(int size)
{
if (size == 1)
{
return Value_FromRegister(Registers_GetFree());
}
if (size == 2)
{
return Value_FromRegisters(Registers_GetFree(), Registers_GetFree());
}
assert(false);
}
Value Value_GetLowerWord(const Value* value)
{
assert(value->addressType == AddressType_Literal || value->size == 2 ||
(value->addressType == AddressType_Register && value->address == -1));
Value retval = *value;
retval.size = 1;
switch (retval.addressType)
{
case AddressType_Literal:
retval.address &= 0xFFFF;
break;
case AddressType_Register:
if (retval.address != -1)
retval.address = (int32_t)(((int16_t)retval.address) % 8);
break;
default:
break;
}
return retval;
}
Value Value_GetUpperWord(const Value* value, bool* oReadOnly)
{
assert(value->addressType == AddressType_Literal || value->size == 2 ||
(value->addressType == AddressType_Register && value->address == -1));
Value retval = *value;
switch (retval.addressType)
{
case AddressType_Register:
if (retval.address != -1)
retval.address = (int32_t)(((int16_t)retval.address) / 8);
*oReadOnly = true;
break;
case AddressType_Memory:
retval.address += 1;
*oReadOnly = true;
break;
case AddressType_Literal:
#ifndef CUSTOM_COMP
retval.address >>= 16;
#endif
*oReadOnly = true;
break;
case AddressType_MemoryRegister:
retval = Value_Register(1);
OutWrite("add r%i, r%i, 1\n", Value_GetR0(&retval), Value_GetR0(value));
retval.addressType = AddressType_MemoryRegister;
*oReadOnly = false;
break;
case AddressType_MemoryRelative:
retval.address -= 1;
*oReadOnly = true;
break;
default:
assert(0);
}
retval.size = 1;
return retval;
}
void Value_FreeValue(Value* value)
{
if (value->addressType == AddressType_Register || value->addressType == AddressType_MemoryRegister)
{
assert(Value_GetR0(value) != -1);
Register_Free(Value_GetR0(value));
if (value->size == 2 && value->addressType != AddressType_MemoryRegister)
Register_Free(Value_GetR1(value));
if (value->addressType == AddressType_Register)
assert(value->size <= 2);
}
// Not freeing anything from the stack anymore unless scope ends!
// (We only free values on top of the stack, for lower values we would have to
// shift everything above down which takes too long. Still very useful to remove
// temporary stack allocations.)
/*if(value->addressType == AddressType_MemoryRelative && value->address == value->size)
{
ShiftAddressSpace(variables, -value->size);
curStackPointerOffset += value->size;
curFuncStackSize -= value->size;
}*/
}
Value Value_MemoryRelative(int addr, int size)
{
return (Value){(int32_t)addr, AddressType_MemoryRelative, size};
}
Value Value_Memory(uint16_t addr, int size)
{
return (Value){(int32_t)addr, AddressType_Memory, size};
}
Value Value_Literal(int32_t literal)
{
#ifndef CUSTOM_COMP
return (Value){literal, AddressType_Literal, (uint32_t)literal > 0xFFFF ? 2 : 1};
#endif
// TODO replace this with ternary once implemented.
#ifdef CUSTOM_COMP
int size;
if (literal > 0xFFFF)
size = 2;
else
size = 1;
return (Value){literal, AddressType_Literal, size};
#endif
}
void Value_GenerateMemCpy(Value dstValue, Value srcValue)
{
if (Value_Equals(&dstValue, &srcValue))
return;
// We can only quickly access values below the sp, so it make sense to move it to the top of the stack
// when a value is above it.
// if ((srcValue.addressType == AddressType_MemoryRelative && GetStackPointerDelta(srcValue.address) < 0) ||
// (dstValue.addressType == AddressType_MemoryRelative && GetStackPointerDelta(dstValue.address) < 0))
// AlignStack();
// Quickly handle Literals
if (srcValue.addressType == AddressType_Literal)
{
if (dstValue.addressType == AddressType_Register)
{
OutWrite("mov r%i, %i\n", Value_GetR0((&dstValue)), srcValue.address & 0xFFFF);
if (dstValue.size == 2)
OutWrite("mov r%i, %i\n", Value_GetR1(&dstValue), srcValue.address >> 16);
}
else if (dstValue.addressType == AddressType_Memory)
{
int temp = Registers_GetFree();
OutWrite("mov r%i, %i\n", temp, srcValue.address & 0xFFFF);
OutWrite("mov [%i], r%i\n", dstValue.address, temp);
if (dstValue.size == 2)
{
OutWrite("mov r%i, %i\n", temp, srcValue.address & 0xFFFF);
OutWrite("mov [%i], r%i\n", dstValue.address + 1, srcValue.address >> 16);
}
Register_Free(temp);
}
else if (dstValue.addressType == AddressType_MemoryRelative)
{
Stack_ToAddress((int)dstValue.address);
OutWrite("mov [sp++], %i\n", srcValue.address & 0xFFFF);
Stack_Offset(1);
if (dstValue.size == 2)
{
OutWrite("mov [sp++], %i\n", srcValue.address >> 16);
Stack_Offset(1);
}
}
else if (dstValue.addressType == AddressType_MemoryRegister)
{
OutWrite("mov [r%i], %i\n", Value_GetR0(&dstValue), srcValue.address & 0xFFFF);
if (dstValue.size == 2)
{
OutWrite("add r%i, 1\n", Value_GetR0(&dstValue));
OutWrite("mov [r%i], %i\n", Value_GetR0(&dstValue), srcValue.address >> 16);
OutWrite("sub r%i, 1\n", Value_GetR0(&dstValue));
}
}
else
Error("Internal error!");
return;
}
int size = dstValue.size;
if (srcValue.size < size)
size = srcValue.size;
int regs[8] = {-1, -1, -1, -1, -1, -1, -1, -1};
int block_size;
if (srcValue.addressType == AddressType_Register)
{
block_size = srcValue.size;
regs[0] = Value_GetR0(&srcValue);
if (srcValue.size == 2)
regs[1] = Value_GetR1(&srcValue);
}
else if (dstValue.addressType == AddressType_Register)
{
block_size = dstValue.size;
regs[0] = Value_GetR0(&dstValue);
if (dstValue.size == 2)
regs[1] = Value_GetR1(&dstValue);
}
else
{
// block size > 1 only really necessary when we're copying from/to a far away location on the stack
if ((srcValue.addressType == AddressType_MemoryRelative && Stack_GetDelta((int)srcValue.address) >= 256) ||
(dstValue.addressType == AddressType_MemoryRelative && Stack_GetDelta((int)dstValue.address) >= 256))
{
block_size = Registers_GetNumFree();
if (block_size > size)
block_size = size;
}
else
block_size = 1;
assert(block_size >= 1);
for (int i = 0; i < block_size; i++)
regs[i] = Registers_GetFree();
}
for (int i = 0; i < size; NULL)
{
// if (srcValue.addressType == AddressType_MemoryRelative)
// StackPointerToAddress(srcValue.address - i);
if (srcValue.addressType != AddressType_Register)
for (int j = 0; j < block_size; j++)
{
if (i + j >= size)
break;
if (srcValue.addressType == AddressType_MemoryRelative)
{
int delta = Stack_GetDelta((int)srcValue.address);
if (delta >= 0 && delta <= 255)
{
OutWrite("mov r%i, [sp-%i]\n", regs[j], delta);
}
else
{
Stack_ToAddress((int)srcValue.address);
if (size > 1)
{
OutWrite("mov r%i, [sp++]\n", regs[j]);
Stack_Offset(1);
}
else
{
OutWrite("mov r%i, [sp]\n", regs[j]);
}
}
(srcValue.address)--;
}
else if (srcValue.addressType == AddressType_MemoryRegister)
{
OutWrite("mov r%i, [r%i]\n", regs[j], Value_GetR0(&srcValue));
if (size - i - j != 1)
OutWrite("add r%i, 1\n", Value_GetR0(&srcValue));
}
else if (srcValue.addressType == AddressType_Memory)
{
OutWrite("mov r%i, [%i]\n", regs[j], srcValue.address++);
}
}
// if (dstValue.addressType == AddressType_MemoryRelative)
// StackPointerToAddress(dstValue.address - i);
if (dstValue.addressType != AddressType_Register || srcValue.addressType == AddressType_Register)
for (int j = 0; j < block_size; j++)
{
if (i + j >= size)
break;
if (dstValue.addressType == AddressType_MemoryRelative)
{
int delta = Stack_GetDelta((int)dstValue.address);
if (delta > 0 && delta <= 255)
{
OutWrite("mov [sp-%i], r%i\n", delta, regs[j]);
}
else
{
Stack_ToAddress((int)dstValue.address);
// if (size > 1)
{
OutWrite("mov [sp++], r%i\n", regs[j]);
Stack_Offset(1);
}
/*else
{
OutWrite("mov [sp], r%i\n", regs[j]);
}*/
}
dstValue.address--;
}
else if (dstValue.addressType == AddressType_MemoryRegister)
{
OutWrite("mov [r%i], r%i\n", Value_GetR0(&dstValue), regs[j]);
if (size - i - j != 1)
OutWrite("add r%i, 1\n", Value_GetR0(&dstValue));
}
else if (dstValue.addressType == AddressType_Memory)
{
OutWrite("mov [%i], r%i\n", dstValue.address++, regs[j]);
}
else if (dstValue.addressType == AddressType_Register)
{
if (j == 0)
OutWrite("mov r%i, r%i\n", Value_GetR0(&dstValue), regs[j]);
else if (j == 1)
OutWrite("mov r%i, r%i\n", Value_GetR1(&dstValue), regs[j]);
}
}
i += block_size;
}
// TODO this is unnecessaty for read-only
if (srcValue.addressType == AddressType_MemoryRegister && size > 1)
OutWrite("sub r%i, %i\n", Value_GetR0(&srcValue), size - 1);
if (dstValue.addressType == AddressType_MemoryRegister && size > 1)
OutWrite("sub r%i, %i\n", Value_GetR0(&dstValue), size - 1);
// If we allocated registers previously, we free them
if (srcValue.addressType != AddressType_Register && dstValue.addressType != AddressType_Register)
for (int i = 0; i < block_size; i++)
Register_Free(regs[i]);
}
void Value_Push(Value* value)
{
Stack_Align();
if (value->addressType == AddressType_Register)
{
OutWrite("mov [sp++], r%i\n", Value_GetR0(value));
if (value->size == 2)
OutWrite("mov [sp++], r%i\n", Value_GetR1(value));
assert(value->size == 1 || value->size == 2);
}
else if (value->addressType == AddressType_Memory)
{
int tmp = Registers_GetFree();
int32_t addr = value->address;
for (int i = 0; i < value->size; i++)
{
OutWrite("mov r%i, [%i]\n", tmp, addr++);
OutWrite("mov [sp++], r%i\n", tmp);
}
Register_Free(tmp);
}
else if (value->addressType == AddressType_MemoryRelative || value->addressType == AddressType_MemoryRegister)
{
Value dst = Value_MemoryRelative(0, value->size);
Value_GenerateMemCpy(dst, *value);
Stack_Offset(-value->size);
}
else if (value->addressType == AddressType_Literal)
{
assert(value->size == 1 || value->size == 2);
OutWrite("mov [sp++], %i\n", value->address & 0xFFFF);
if (value->size == 2)
OutWrite("mov [sp++], %i\n", value->address >> 16);
}
Stack_OffsetSize(value->size);
}
void Value_MemSet(Value* value, uint16_t n)
{
if (value->addressType == AddressType_Register)
{
OutWrite("mov r%i, %i\n", Value_GetR0(value), n);
if (value->size == 2)
OutWrite("mov r%i, %i\n", Value_GetR1(value), n);
}
if (value->addressType == AddressType_Memory)
{
for (int i = 0; i < value->size; i++)
OutWrite("mov [%i], %i\n", value->address + (int32_t)i, n);
}
if (value->addressType == AddressType_MemoryRelative)
{
Stack_ToAddress((int)value->address);
for (int i = 0; i < value->size; i++)
{
OutWrite("mov [sp++], %i\n", n);
Stack_Offset(1);
}
}
}
void PrintValueAsOperand(const Value* val)
{
switch (val->addressType)
{
case AddressType_Register:
// TODO put this in the switch, once the implemenation
// doesn't create a jump table from 8 to 0xFFFF
if (val->address == -1)
{
OutWrite("rz"); // Zero register
return;
}
switch ((int)val->address)
{
case Register_IP:
OutWrite("ip");
return;
case Register_SP:
OutWrite("sp");
return;
default:
OutWrite("r%i", val->address);
return;
}
break;
case AddressType_Memory:
OutWrite("[%i]", val->address);
break;
case AddressType_MemoryRegister:
OutWrite("[r%i]", val->address);
break;
case AddressType_MemoryRelative:;
int delta = Stack_GetOffset() + (int)val->address;
assert(delta >= 0);
if (delta == 0 && val->address == 0)
{
// If the address is at the top of the stack, increment
// the sp as well, to keep the stack aligned.
OutWrite("[sp++]", delta);
Stack_Offset(1);
}
else if (delta == 0)
OutWrite("[sp]", delta);
else
OutWrite("[sp-%i]", delta);
break;
case AddressType_Literal:
OutWrite("%i", val->address);
break;
default:
assert(0);
}
}
// Stores whether the value is true or false in the zero flag.
// Used for branches.
void Value_ToFlag(Value* value)
{
if (value->addressType == AddressType_Register)
{
if (value->size == 1)
OutWrite("add r%i, 0\n", Value_GetR0(value));
else if (value->size == 2)
{
int temp = Registers_GetFree();
OutWrite("mov r%i, r%i\n", temp, Value_GetR0(value));
OutWrite("or r%i, r%i\n", temp, Value_GetR1(value));
Register_Free(temp);
}
return;
}
if (value->addressType == AddressType_Memory)
{
if (value->size == 1)
OutWrite("add [%i], rz\n", value->address);
else if (value->size == 2)
{
int temp = Registers_GetFree();
OutWrite("mov r%i, [%i]\n", temp, value->address);
OutWrite("or r%i, [%i]\n", temp, value->address + (int32_t)1);
Register_Free(temp);
}
return;
}
if (value->addressType == AddressType_MemoryRegister)
{
if (value->size == 1)
OutWrite("add [r%i], 0\n", Value_GetR0(value));
else if (value->size == 2)
{
int temp = Registers_GetFree();
OutWrite("mov r%i, [r%i]\n", temp, Value_GetR0(value));
OutWrite("add r%i, 1\n", Value_GetR0(value));
OutWrite("or r%i, [r%i]\n", temp, Value_GetR0(value));
OutWrite("sub r%i, 1\n", Value_GetR0(value));
Register_Free(temp);
}
return;
}
if (value->addressType == AddressType_MemoryRelative)
{
if (value->size == 1)
{
int delta = Stack_GetDelta((int)value->address);
if (delta <= 255 && delta > 0)
{
OutWrite("add rz, [sp-%u]\n", delta);
}
else
{
Stack_ToAddress((int)value->address);
OutWrite("add rz, [sp]\n");
}
}
// todo make this sp-relative too
else if (value->size == 2)
{
Stack_ToAddress((int)value->address);
int temp = Registers_GetFree();
OutWrite("mov r%i, [sp]\n", temp);
Stack_ToAddress((int)value->address + 1);
OutWrite("or r%i, [sp]\n", temp);
Register_Free(temp);
}
return;
}
if (value->addressType == AddressType_Literal)
{
if (value->address != 0)
OutWrite("add rz, 1\n");
else
OutWrite("add rz, 0\n");
return;
}
Error("Cannot use type as bool!");
}
bool Value_Equals(const Value* a, const Value* b)
{
return a->address == b->address && a->addressType == b->addressType && a->size == b->size;
}
Value Value_Flag(Flag f)
{
return (Value){(int32_t)f, AddressType_Flag, -1};
}