-
Notifications
You must be signed in to change notification settings - Fork 9
/
addressobj.c
672 lines (632 loc) · 20 KB
/
addressobj.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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
/*
$Id: addressobj.c 3086 2023-09-03 06:23:08Z soci $
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "addressobj.h"
#include <string.h>
#include "error.h"
#include "eval.h"
#include "variables.h"
#include "arguments.h"
#include "instruction.h"
#include "boolobj.h"
#include "strobj.h"
#include "intobj.h"
#include "typeobj.h"
#include "noneobj.h"
#include "errorobj.h"
#include "floatobj.h"
#include "bitsobj.h"
#include "bytesobj.h"
#include "registerobj.h"
static Type obj;
Type *const ADDRESS_OBJ = &obj;
static MUST_CHECK Obj *address_from_obj(Obj *v1, linepos_t epoint) {
switch (v1->obj->type) {
case T_BOOL:
case T_INT:
case T_BITS:
case T_FLOAT:
case T_BYTES:
case T_STR:
return new_address(val_reference(v1), A_NONE);
case T_NONE:
case T_ERROR:
case T_ADDRESS: return val_reference(v1);
default: break;
}
return new_error_conv(v1, ADDRESS_OBJ, epoint);
}
static MUST_CHECK Obj *convert(oper_t op) {
return address_from_obj(op->v2, op->epoint2);
}
static FAST_CALL void destroy(Obj *o1) {
Address *v1 = Address(o1);
val_destroy(v1->val);
}
static FAST_CALL void garbage(Obj *o1, int i) {
Address *v1 = Address(o1);
Obj *v;
switch (i) {
case -1:
v1->val->refcount--;
return;
case 0:
return;
case 1:
v = v1->val;
if ((v->refcount & SIZE_MSB) != 0) {
v->refcount -= SIZE_MSB - 1;
v->obj->garbage(v, 1);
} else v->refcount++;
return;
}
}
static FAST_CALL bool same(const Obj *o1, const Obj *o2) {
const Address *v1 = Address(o1), *v2 = Address(o2);
return o1->obj == o2->obj && v1->type == v2->type && v1->val->obj->same(v1->val, v2->val);
}
static MUST_CHECK Obj *truth(Obj *o1, Truth_types type, linepos_t epoint) {
Address *v1 = Address(o1);
Obj *v;
if (v1->type != A_NONE && v1->val != none_value && v1->val->obj != ERROR_OBJ) {
return DEFAULT_OBJ->truth(o1, type, epoint);
}
v = v1->val;
return v->obj->truth(v, type, epoint);
}
static MUST_CHECK Obj *hash(Obj *o1, int *hs, linepos_t epoint) {
Address *v1 = Address(o1);
Obj *v = v1->val;
Obj *err = v->obj->hash(v, hs, epoint);
if (err == NULL) {
*hs = ((unsigned int)*hs + v1->type) & ((~0U) >> 1);
}
return err;
}
static MUST_CHECK Obj *repr(Obj *o1, linepos_t epoint, size_t maxsize) {
Address *v1 = Address(o1);
uint8_t *s;
size_t len, chars;
char buffer[100], buffer2[100], *b2;
atype_t addrtype;
unsigned int ind, ind2;
Obj *tmp;
Str *v, *str;
ind2 = 0;
addrtype = v1->type;
ind = 99;
buffer2[ind] = '\0';
if (addrtype == A_NONE) {
ind -= 8;
memcpy(&buffer2[ind], "address(", 8);
buffer[ind2++] = ')';
} else {
while ((addrtype & MAX_ADDRESS_MASK) != 0) {
uint32_t mode;
switch ((Address_types)((addrtype & 0xf000) >> 12)) {
case A_XR: mode = (',' << 16) + ('x' << 24);break;
case A_YR: mode = (',' << 16) + ('y' << 24);break;
case A_ZR: mode = (',' << 16) + ('z' << 24);break;
case A_SR: mode = (',' << 16) + ('s' << 24);break;
case A_RR: mode = (',' << 16) + ('r' << 24);break;
case A_DR: mode = (',' << 16) + ('d' << 24);break;
case A_BR: mode = (',' << 16) + ('b' << 24);break;
case A_KR: mode = (',' << 16) + ('k' << 24);break;
case A_I: mode = ('(' << 8) + (')' << 16);break;
case A_LI: mode = ('[' << 8) + (']' << 16);break;
case A_IMMEDIATE_SIGNED: mode = ('#' << 8) + '+';break;
case A_IMMEDIATE: mode = '#' << 8;break;
default: mode = 0;
}
if ((char)mode != '\0') buffer2[--ind] = (char)mode;
mode >>= 8;
if ((char)mode != '\0') buffer2[--ind] = (char)mode;
mode >>= 8;
if ((char)mode != '\0') buffer[ind2++] = (char)mode;
mode >>= 8;
if ((char)mode != '\0') buffer[ind2++] = (char)mode;
addrtype <<= 4;
}
}
b2 = buffer2 + ind;
ind = 99 - ind;
chars = ind + ind2;
if (chars > maxsize) return NULL;
tmp = v1->val->obj->repr(v1->val, epoint, maxsize - chars);
if (tmp == NULL || tmp->obj != STR_OBJ) return tmp;
str = Str(tmp);
if (add_overflow(str->len, chars, &len)) goto error;
chars += str->chars;
if (chars > maxsize) {
error:
val_destroy(tmp);
return NULL;
}
if (ind != 0 && str->len != 0 && b2[ind - 1] == '+' && str->data[0] == '-') {
ind--; len--; chars--;
}
v = new_str2(len);
if (v == NULL) goto error;
v->chars = chars;
s = v->data;
if (ind != 0) {
memcpy(s, b2, ind);
s += ind;
}
if (str->len != 0) {
memcpy(s, str->data, str->len);
s += str->len;
}
if (ind2 != 0) memcpy(s, buffer, ind2);
val_destroy(tmp);
return Obj(v);
}
static MUST_CHECK Obj *str(Obj *o1, linepos_t epoint, size_t maxsize) {
Address *v1 = Address(o1);
if (v1->type == A_NONE) {
o1 = v1->val;
return o1->obj->repr(o1, epoint, maxsize);
}
return repr(o1, epoint, maxsize);
}
bool check_addr(atype_t type) {
while (type != A_NONE) {
switch ((Address_types)(type & 0xf)) {
case A_I:
case A_LI: return true;
case A_IMMEDIATE:
case A_IMMEDIATE_SIGNED:
case A_KR:
case A_DR:
case A_BR:
case A_XR:
case A_YR:
case A_ZR:
case A_RR:
case A_SR:
case A_NONE: break;
}
type >>= 4;
}
return false;
}
static inline bool check_addr2(atype_t type) {
while (type != A_NONE) {
switch ((Address_types)(type & 0xf)) {
case A_KR:
case A_DR:
case A_BR:
case A_XR:
case A_YR:
case A_ZR:
case A_RR:
case A_SR:
case A_I:
case A_LI: return true;
case A_IMMEDIATE:
case A_IMMEDIATE_SIGNED:
case A_NONE: break;
}
type >>= 4;
}
return false;
}
Address_types register_to_indexing(unsigned int c) {
switch (c) {
case 's': return A_SR;
case 'r': return A_RR;
case 'z': return A_ZR;
case 'y': return A_YR;
case 'x': return A_XR;
case 'd': return A_DR;
case 'b': return A_BR;
case 'k': return A_KR;
default: return A_NONE;
}
}
static FAST_CALL uint32_t address(const Obj *o1) {
const Address *v1 = Address(o1);
Obj *v = v1->val;
uint32_t am = v->obj->address(v);
atype_t type = v1->type;
while (type != A_NONE) {
am <<= 4;
type >>= 4;
}
return am | v1->type;
}
static MUST_CHECK Error *ival(Obj *o1, ival_t *iv, unsigned int bits, linepos_t epoint) {
Address *v1 = Address(o1);
Obj *v;
if (v1->type != A_NONE && v1->val != none_value && v1->val->obj != ERROR_OBJ) {
return DEFAULT_OBJ->ival(o1, iv, bits, epoint);
}
v = v1->val;
return v->obj->ival(v, iv, bits, epoint);
}
static MUST_CHECK Error *uval(Obj *o1, uval_t *uv, unsigned int bits, linepos_t epoint) {
Address *v1 = Address(o1);
Obj *v;
if (v1->type != A_NONE && v1->val != none_value && v1->val->obj != ERROR_OBJ) {
return DEFAULT_OBJ->uval(o1, uv, bits, epoint);
}
v = v1->val;
return v->obj->uval(v, uv, bits, epoint);
}
static MUST_CHECK Error *uval2(Obj *o1, uval_t *uv, unsigned int bits, linepos_t epoint) {
Address *v1 = Address(o1);
Obj *v;
if (v1->type != A_NONE && v1->val != none_value && v1->val->obj != ERROR_OBJ) {
return DEFAULT_OBJ->uval2(o1, uv, bits, epoint);
}
v = v1->val;
return v->obj->uval2(v, uv, bits, epoint);
}
static MUST_CHECK Error *iaddress(Obj *o1, ival_t *iv, unsigned int bits, linepos_t epoint) {
Address *v1 = Address(o1);
Obj *v = v1->val;
return v->obj->iaddress(v, iv, bits, epoint);
}
static MUST_CHECK Error *uaddress(Obj *o1, uval_t *uv, unsigned int bits, linepos_t epoint) {
Address *v1 = Address(o1);
Obj *v = v1->val;
return v->obj->uaddress(v, uv, bits, epoint);
}
MUST_CHECK Obj *float_from_address(Address *v1, linepos_t epoint) {
if (v1->type != A_NONE && v1->val != none_value && v1->val->obj != ERROR_OBJ) {
return new_error_conv(Obj(v1), FLOAT_OBJ, epoint);
}
return float_from_obj(v1->val, epoint);
}
MUST_CHECK Obj *int_from_address(Address *v1, linepos_t epoint) {
if (v1->type != A_NONE && v1->val != none_value && v1->val->obj != ERROR_OBJ) {
return new_error_conv(Obj(v1), INT_OBJ, epoint);
}
return int_from_obj(v1->val, epoint);
}
MUST_CHECK Obj *bits_from_address(Address *v1, linepos_t epoint) {
if (v1->type != A_NONE && v1->val != none_value && v1->val->obj != ERROR_OBJ) {
return new_error_conv(Obj(v1), BITS_OBJ, epoint);
}
return bits_from_obj(v1->val, epoint);
}
MUST_CHECK Obj *bytes_from_address(Address *v1, linepos_t epoint) {
if (v1->type != A_NONE && v1->val != none_value && v1->val->obj != ERROR_OBJ) {
return new_error_conv(Obj(v1), BYTES_OBJ, epoint);
}
return bytes_from_obj(v1->val, epoint);
}
static MUST_CHECK Obj *sign(Obj *o1, linepos_t epoint) {
Address *v1 = Address(o1);
Obj *v;
if (v1->type != A_NONE && v1->val != none_value && v1->val->obj != ERROR_OBJ) {
return DEFAULT_OBJ->sign(o1, epoint);
}
v = v1->val;
return v->obj->sign(v, epoint);
}
static MUST_CHECK Obj *function(oper_t op) {
Address *v1 = Address(op->v2);
Obj *v;
if (v1->type != A_NONE && v1->val != none_value && v1->val->obj != ERROR_OBJ) {
return DEFAULT_OBJ->function(op);
}
op->v2 = v = v1->val;
op->inplace = op->inplace == Obj(v1) && v->refcount == 1 ? v : NULL;
return v->obj->function(op);
}
static MUST_CHECK Obj *calc1(oper_t op) {
Obj *result;
Address *v1 = Address(op->v1);
atype_t am = v1->type;
switch (op->op) {
case O_LNOT:
case O_STRING:
if (am != A_NONE) break;
FALL_THROUGH; /* fall through */
case O_BANK:
case O_HIGHER:
case O_LOWER:
case O_HWORD:
case O_WORD:
case O_BSWORD:
case O_INV:
case O_NEG:
case O_POS:
if (check_addr2(am)) break;
op->v1 = v1->val;
op->inplace = NULL;
result = op->v1->obj->calc1(op);
if (am == A_NONE) return result;
return new_address(result, am);
default: break;
}
if (v1->val == none_value || v1->val->obj == ERROR_OBJ) return val_reference(v1->val);
return obj_oper_error(op);
}
static MUST_CHECK Obj *slice(oper_t op, argcount_t indx) {
Obj *val = Address(op->v1)->val;
if (val == none_value || val->obj == ERROR_OBJ) {
return val_reference(val);
}
return DEFAULT_OBJ->slice(op, indx);
}
static MUST_CHECK Obj *calc2(oper_t op) {
Obj *o2 = op->v2, *result;
Address *v1 = Address(op->v1);
atype_t am;
if (op->op == O_LAND || op->op == O_LOR || op->op == O_LXOR) {
bool i;
result = truth(Obj(v1), TRUTH_BOOL, op->epoint);
if (result->obj != BOOL_OBJ) return result;
i = Bool(result)->value;
val_destroy(result);
if (diagnostics.strict_bool) err_msg_bool_oper(op);
if (op->op == O_LXOR) return calc2_lxor(op, i);
return val_reference(i != (op->op == O_LOR) ? o2 : Obj(v1));
}
switch (o2->obj->type) {
case T_ADDRESS:
{
Address *v2 = Address(o2);
atype_t am2 = v2->type;
am = v1->type;
switch (op->op) {
case O_CMP:
case O_EQ:
case O_NE:
case O_MIN:
case O_LT:
case O_LE:
case O_MAX:
case O_GT:
case O_GE:
if (am == am2) {
op->v1 = v1->val;
op->v2 = v2->val;
op->inplace = NULL;
return op->v1->obj->calc2(op);
}
return obj_oper_compare(op, (am < am2) ? -1 : 1);
case O_ADD:
if (check_addr(am)) break;
if (check_addr(am2)) break;
op->v1 = v1->val;
op->v2 = v2->val;
op->inplace = NULL;
result = op->v1->obj->calc2(op);
if (am == A_NONE && v2->type == A_NONE) return result;
while ((Address_types)(am2 & 0xf) != A_NONE) { am <<= 4; am2 >>= 4; }
return new_address(result, am | v2->type);
case O_SUB:
if (check_addr(am)) break;
if (check_addr(am2)) break;
{
atype_t am1 = A_NONE;
for (; (am & MAX_ADDRESS_MASK) != 0; am <<= 4) {
atype_t amc = (am >> 12) & 0xf;
atype_t am3, am4;
if (amc == A_NONE) continue;
am3 = A_NONE; am4 = am2;
while (am4 != A_NONE) {
atype_t am5 = (am4 & 0xf);
if (amc == am5) amc = A_NONE;
else am3 = (am3 << 4) | am5;
am4 >>= 4;
}
am2 = am3;
if (amc == A_NONE) continue;
am1 = (am1 << 4) | amc;
}
if (am2 != A_NONE) break;
op->v1 = v1->val;
op->v2 = v2->val;
op->inplace = NULL;
result = op->v1->obj->calc2(op);
if (am1 == A_NONE) return result;
return new_address(result, am1);
}
case O_XOR:
if (check_addr(am)) break;
if (check_addr(am2)) break;
if (am == am2) {
op->v1 = v1->val;
op->v2 = v2->val;
op->inplace = NULL;
return op->v1->obj->calc2(op);
}
break;
default:
break;
}
break;
}
case T_BOOL:
case T_INT:
case T_BITS:
case T_FLOAT:
case T_BYTES:
case T_STR:
am = v1->type;
switch (op->op) {
case O_CMP:
case O_EQ:
case O_NE:
case O_MIN:
case O_LT:
case O_LE:
case O_MAX:
case O_GT:
case O_GE:
if (am == A_NONE || (am == A_DR && dpage == 0) || (am == A_BR && databank == 0)) {
op->v1 = v1->val;
op->inplace = NULL;
return op->v1->obj->calc2(op);
}
break;
default:
if (am == A_NONE) {
op->v1 = v1->val;
op->inplace = NULL;
return op->v1->obj->calc2(op);
}
if (check_addr2(am)) break;
goto ok;
case O_ADD:
case O_SUB:
if (check_addr(am)) break;
ok:
op->v1 = v1->val;
op->inplace = NULL;
return new_address(op->v1->obj->calc2(op), am);
}
break;
case T_REGISTER:
if (Register(op->v2)->len == 1) {
Address_types am2 = register_to_indexing(Register(op->v2)->data[0]);
if (am2 == A_NONE) break;
if (op->op == O_ADD) {
return new_address(val_reference(v1->val), v1->type << 4 | am2);
}
if (op->op == O_SUB) {
atype_t am1 = A_NONE;
for (am = v1->type; ; am >>= 4) {
atype_t amc = am & 0xf;
switch (amc) {
case A_I:
case A_LI:
case A_IMMEDIATE:
case A_IMMEDIATE_SIGNED:
if (am2 == A_NONE) {
am1 = (am1 >> 4) | (amc << 12);
continue;
}
break;
case A_KR:
case A_DR:
case A_BR:
case A_XR:
case A_YR:
case A_ZR:
case A_RR:
case A_SR:
if ((Address_types)amc == am2) {
am2 = A_NONE;
continue;
}
am1 = (am1 >> 4) | (amc << 12);
continue;
default: break;
}
break;
}
if (am2 != A_NONE) break;
if (am1 != A_NONE) while ((am1 & 0xf) == A_NONE) am1 >>= 4;
return new_address(val_reference(v1->val), am1);
}
}
break;
default:
if (op->op != O_MEMBER && op->op != O_X) {
return o2->obj->rcalc2(op);
}
if (o2 == none_value || o2->obj == ERROR_OBJ) return val_reference(o2);
}
if (v1->val == none_value || v1->val->obj == ERROR_OBJ) return val_reference(v1->val);
return obj_oper_error(op);
}
static MUST_CHECK Obj *rcalc2(oper_t op) {
const Type *t1 = op->v1->obj;
Address *v2 = Address(op->v2);
atype_t am;
switch (t1->type) {
case T_BOOL:
case T_INT:
case T_BITS:
case T_FLOAT:
case T_BYTES:
case T_GAP:
am = v2->type;
switch (op->op) {
default:
if (am == A_NONE) {
op->v2 = v2->val;
op->inplace = NULL;
return t1->calc2(op);
}
break;
case O_MUL:
case O_OR:
case O_XOR:
case O_AND:
if (check_addr2(am)) break;
goto ok;
case O_ADD:
if (check_addr(am)) break;
ok:
op->v2 = v2->val;
op->inplace = NULL;
return new_address(t1->calc2(op), am);
}
break;
case T_CODE:
return t1->calc2(op);
case T_REGISTER:
if (Register(op->v1)->len == 1) {
am = register_to_indexing(Register(op->v1)->data[0]);
if (am == A_NONE) break;
if (op->op == O_ADD) {
return new_address(val_reference(v2->val), v2->type << 4 | am);
}
if (op->op == O_SUB) {
if (am == v2->type) {
op->v1 = int_value[0];
op->v2 = v2->val;
op->inplace = NULL;
return INT_OBJ->calc2(op);
}
}
}
break;
default: break;
}
if (v2->val == none_value || v2->val->obj == ERROR_OBJ) return val_reference(v2->val);
return obj_oper_error(op);
}
void addressobj_init(void) {
Type *type = new_type(&obj, T_ADDRESS, "address", sizeof(Address));
type->convert = convert;
type->destroy = destroy;
type->garbage = garbage;
type->same = same;
type->truth = truth;
type->hash = hash;
type->repr = repr;
type->str = str;
type->address = address;
type->ival = ival;
type->uval = uval;
type->uval2 = uval2;
type->iaddress = iaddress;
type->uaddress = uaddress;
type->sign = sign;
type->function = function;
type->calc1 = calc1;
type->calc2 = calc2;
type->rcalc2 = rcalc2;
type->slice = slice;
}
void addressobj_names(void) {
new_builtin("address", val_reference(Obj(ADDRESS_OBJ)));
}