forked from StarWolf3000/vasm-mirror
-
Notifications
You must be signed in to change notification settings - Fork 0
/
atom.c
661 lines (546 loc) · 14.2 KB
/
atom.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
/* atom.c - atomic objects from source */
/* (c) in 2010-2019 by Volker Barthelmann and Frank Wille */
#include "vasm.h"
/* searches mnemonic list and tries to parse (via the cpu module)
the operands according to the mnemonic requirements; returns an
instruction or 0 */
instruction *new_inst(char *inst,int len,int op_cnt,char **op,int *op_len)
{
#if MAX_OPERANDS!=0
operand ops[MAX_OPERANDS];
int j,k,mnemo_opcnt,omitted,skipped;
#endif
int i,inst_found=0;
hashdata data;
instruction *new;
new = mymalloc(sizeof(*new));
#if HAVE_INSTRUCTION_EXTENSION
init_instruction_ext(&new->ext);
#endif
#if MAX_OPERANDS!=0 && NEED_CLEARED_OPERANDS!=0
/* reset operands to allow the cpu-backend to parse them only once */
memset(ops,0,sizeof(ops));
#endif
if (find_namelen_nc(mnemohash,inst,len,&data)) {
i = data.idx;
/* try all mnemonics with the same name until operands match */
do {
inst_found = 1;
if (!MNEMONIC_VALID(i)) {
i++;
continue; /* try next */
}
#if MAX_OPERANDS!=0
#if ALLOW_EMPTY_OPS
mnemo_opcnt = op_cnt<MAX_OPERANDS ? op_cnt : MAX_OPERANDS;
#else
for (j=0; j<MAX_OPERANDS; j++)
if (mnemonics[i].operand_type[j] == 0)
break;
mnemo_opcnt = j; /* number of expected operands for this mnemonic */
#endif
inst_found = 2;
save_symbols(); /* make sure we can restore symbols to this point */
for (j=k=omitted=skipped=0; j<mnemo_opcnt; j++) {
if (op_cnt+omitted < mnemo_opcnt &&
OPERAND_OPTIONAL(&ops[j],mnemonics[i].operand_type[j])) {
omitted++;
}
else {
int rc;
if (k >= op_cnt) /* missing mandatory operands */
break;
rc = parse_operand(op[k],op_len[k],&ops[j],
mnemonics[i].operand_type[j]);
if (rc == PO_CORRUPT) {
myfree(new);
restore_symbols();
return 0;
}
if (rc == PO_NOMATCH)
break;
/* MATCH, move to next parsed operand */
k++;
if (rc == PO_SKIP) { /* but skip next operand type from table */
j++;
skipped++;
}
}
}
#if IGNORE_FIRST_EXTRA_OP
if (mnemo_opcnt > 0)
#endif
if (j<mnemo_opcnt || k<op_cnt) {
/* No match. Try next mnemonic. */
i++;
restore_symbols();
continue;
}
/* Matched! Copy operands. */
mnemo_opcnt -= skipped;
for (j=0; j<mnemo_opcnt; j++) {
new->op[j] = mymalloc(sizeof(operand));
*new->op[j] = ops[j];
}
for(; j<MAX_OPERANDS; j++)
new->op[j] = 0;
#endif /* MAX_OPERANDS!=0 */
new->code = i;
return new;
}
while (i<mnemonic_cnt && !strnicmp(mnemonics[i].name,inst,len)
&& mnemonics[i].name[len]==0);
}
switch (inst_found) {
case 1:
general_error(8); /* instruction not supported by cpu */
break;
case 2:
general_error(0); /* illegal operand types */
break;
default:
general_error(1,cnvstr(inst,len)); /* completely unknown mnemonic */
break;
}
myfree(new);
return 0;
}
dblock *new_dblock(void)
{
dblock *new = mymalloc(sizeof(*new));
new->size = 0;
new->data = 0;
new->relocs = 0;
return new;
}
sblock *new_sblock(expr *space,size_t size,expr *fill)
{
sblock *sb = mymalloc(sizeof(sblock));
sb->space = 0;
sb->space_exp = space;
sb->size = size;
if (!(sb->fill_exp = fill))
memset(sb->fill,0,MAXPADBYTES);
sb->relocs = 0;
sb->maxalignbytes = 0;
sb->flags = 0;
return sb;
}
static size_t space_size(sblock *sb,section *sec,taddr pc)
{
utaddr space=0;
if (eval_expr(sb->space_exp,&space,sec,pc) || !final_pass)
sb->space = space;
else
general_error(30); /* expression must be constant */
if (final_pass && sb->fill_exp) {
if (sb->size <= sizeof(taddr)) {
/* space is filled with an expression which may also need relocations */
symbol *base=NULL;
taddr fill;
utaddr i;
if (!eval_expr(sb->fill_exp,&fill,sec,pc)) {
if (find_base(sb->fill_exp,&base,sec,pc)==BASE_ILLEGAL)
general_error(38); /* illegal relocation */
}
copy_cpu_taddr(sb->fill,fill,sb->size);
if (base && !sb->relocs) {
/* generate relocations */
for (i=0; i<space; i++)
add_extnreloc(&sb->relocs,base,fill,REL_ABS,
0,sb->size<<3,sb->size*i);
}
}
else
general_error(30); /* expression must be constant */
}
return sb->size * space;
}
static size_t roffs_size(expr *offsexp,section *sec,taddr pc)
{
taddr offs;
eval_expr(offsexp,&offs,sec,pc);
offs = sec->org + offs - pc;
return offs>0 ? offs : 0;
}
/* adds an atom to the specified section; if sec==0, the current
section is used */
void add_atom(section *sec,atom *a)
{
if (!sec) {
sec = default_section();
if (!sec) {
general_error(3);
return;
}
}
a->changes = 0;
a->src = cur_src;
a->line = cur_src!=NULL ? cur_src->line : 0;
if (sec->last) {
atom *pa = sec->last;
pa->next = a;
/* make sure that a label on the same line gets the same alignment */
if (pa->type==LABEL && pa->line==a->line &&
(a->type==INSTRUCTION || a->type==DATADEF || a->type==SPACE))
pa->align = a->align;
}
else
sec->first = a;
a->next = 0;
sec->last = a;
sec->pc = pcalign(a,sec->pc);
a->lastsize = atom_size(a,sec,sec->pc);
sec->pc += a->lastsize;
if (a->align > sec->align)
sec->align = a->align;
if (listena) {
a->list = last_listing;
if (last_listing) {
if (!last_listing->atom)
last_listing->atom = a;
}
}
else
a->list = 0;
}
size_t atom_size(atom *p,section *sec,taddr pc)
{
switch(p->type) {
case LABEL:
case LINE:
case OPTS:
case PRINTTEXT:
case PRINTEXPR:
case RORG:
case RORGEND:
case ASSERT:
case NLIST: /* it has a size, but not in the current section */
return 0;
case DATA:
return p->content.db->size;
case INSTRUCTION:
return p->content.inst->code>=0?
instruction_size(p->content.inst,sec,pc):0;
case SPACE:
return space_size(p->content.sb,sec,pc);
case DATADEF:
return (p->content.defb->bitsize+7)/8;
case ROFFS:
return roffs_size(p->content.roffs,sec,pc);
default:
ierror(0);
break;
}
return 0;
}
static void print_instruction(FILE *f,instruction *p)
{
int i;
printf("inst %d(%s) ",p->code,p->code>=0?mnemonics[p->code].name:"deleted");
#if MAX_OPERANDS!=0
for (i=0; i<MAX_OPERANDS; i++)
printf("%p ",(void *)p->op[i]);
#endif
}
void print_atom(FILE *f,atom *p)
{
size_t i;
rlist *rl;
switch (p->type) {
case LABEL:
fprintf(f,"symbol: ");
print_symbol(f,p->content.label);
break;
case DATA:
fprintf(f,"data(%lu): ",(unsigned long)p->content.db->size);
for (i=0;i<p->content.db->size;i++)
fprintf(f,"%02x ",p->content.db->data[i]);
for (rl=p->content.db->relocs; rl; rl=rl->next)
print_reloc(f,rl->type,rl->reloc);
break;
case INSTRUCTION:
print_instruction(f,p->content.inst);
break;
case SPACE:
fprintf(f,"space(%lu,fill=",
(unsigned long)(p->content.sb->space*p->content.sb->size));
for (i=0; i<p->content.sb->size; i++)
fprintf(f,"%02x%c",(unsigned char)p->content.sb->fill[i],
(i==p->content.sb->size-1)?')':' ');
for (rl=p->content.sb->relocs; rl; rl=rl->next)
print_reloc(f,rl->type,rl->reloc);
break;
case DATADEF:
fprintf(f,"datadef(%lu bits)",(unsigned long)p->content.defb->bitsize);
break;
case LINE:
fprintf(f,"line: %d of %s",p->content.srcline,getdebugname());
break;
#if HAVE_CPU_OPTS
case OPTS:
print_cpu_opts(f,p->content.opts);
break;
#endif
case PRINTTEXT:
fprintf(f,"text: \"%s\"",p->content.ptext);
break;
case PRINTEXPR:
fprintf(f,"expr: ");
print_expr(f,p->content.pexpr->print_exp);
break;
case ROFFS:
fprintf(f,"roffs: offset ");
print_expr(f,p->content.roffs);
break;
case RORG:
fprintf(f,"rorg: relocate to 0x%llx",ULLTADDR(*p->content.rorg));
break;
case RORGEND:
fprintf(f,"rorg end");
break;
case ASSERT:
fprintf(f,"assert: %s (message: %s)\n",p->content.assert->expstr,
p->content.assert->msgstr?p->content.assert->msgstr:emptystr);
break;
case NLIST:
fprintf(f,"nlist: %s (type %d, other %d, desc %d) with value ",
p->content.nlist->name!=NULL ? p->content.nlist->name : "<NULL>",
p->content.nlist->type,p->content.nlist->other,
p->content.nlist->desc);
if (p->content.nlist->value != NULL)
print_expr(f,p->content.nlist->value);
else
fprintf(f,"NULL");
break;
default:
ierror(0);
}
}
/* prints and formats an expression from a PRINTEXPR atom */
void atom_printexpr(printexpr *pexp,section *sec,taddr pc)
{
taddr t;
long long v;
int i;
eval_expr(pexp->print_exp,&t,sec,pc);
if (pexp->type==PEXP_SDEC && (t&(1LL<<(pexp->size-1)))!=0) {
/* signed decimal */
v = -1;
v &= ~(long long)MAKEMASK(pexp->size);
}
else
v = 0;
v |= t & MAKEMASK(pexp->size);
switch (pexp->type) {
case PEXP_HEX:
printf("%llX",(unsigned long long)v);
break;
case PEXP_SDEC:
printf("%lld",v);
break;
case PEXP_UDEC:
printf("%llu",(unsigned long long)v);
break;
case PEXP_BIN:
for (i=pexp->size-1; i>=0; i--)
putchar((v & (1LL<<i)) ? '1' : '0');
break;
case PEXP_ASC:
for (i=((pexp->size+7)>>3)-1; i>=0; i--) {
unsigned char c = (v>>(i*8))&0xff;
putchar(isprint(c) ? c : '.');
}
break;
default:
ierror(0);
break;
}
}
atom *clone_atom(atom *a)
{
atom *new = mymalloc(sizeof(atom));
void *p;
memcpy(new,a,sizeof(atom));
switch (a->type) {
/* INSTRUCTION and DATADEF have to be cloned as well, because they will
be deallocated and transformed into DATA during assemble() */
case INSTRUCTION:
p = mymalloc(sizeof(instruction));
memcpy(p,a->content.inst,sizeof(instruction));
new->content.inst = p;
break;
case DATADEF:
p = mymalloc(sizeof(defblock));
memcpy(p,a->content.defb,sizeof(defblock));
new->content.defb = p;
break;
default:
break;
}
new->next = 0;
new->src = NULL;
new->line = 0;
new->list = NULL;
return new;
}
atom *add_data_atom(section *sec,size_t sz,taddr alignment,taddr c)
{
dblock *db = new_dblock();
atom *a;
db->size = sz;
db->data = mymalloc(sz);
if (sz > 1)
setval(BIGENDIAN,db->data,sz,c);
else
*(db->data) = c;
a = new_data_atom(db,alignment);
add_atom(sec,a);
return a;
}
void add_leb128_atom(section *sec,taddr c)
{
taddr b;
do {
b = c & 0x7f;
if ((c >>= 7) != 0)
b |= 0x80;
add_data_atom(sec,1,1,b);
} while (c != 0);
}
void add_sleb128_atom(section *sec,taddr c)
{
int done = 0;
taddr b;
do {
b = c & 0x7f;
c >>= 7; /* assumes arithmetic shifts! */
if ((c==0 && !(b&0x40)) || (c==-1 && (b&0x40)))
done = 1;
else
b |= 0x80;
add_data_atom(sec,1,1,b);
} while (!done);
}
atom *add_bytes_atom(section *sec,void *p,size_t sz)
{
dblock *db = new_dblock();
atom *a;
db->size = sz;
db->data = mymalloc(sz);
memcpy(db->data,p,sz);
a = new_data_atom(db,1);
add_atom(sec,a);
return a;
}
static atom *new_atom(int type,taddr align)
{
atom *new = mymalloc(sizeof(*new));
new->next = NULL;
new->type = type;
new->align = align;
return new;
}
atom *new_inst_atom(instruction *p)
{
atom *new = new_atom(INSTRUCTION,inst_alignment);
new->content.inst = p;
return new;
}
atom *new_data_atom(dblock *p,taddr align)
{
atom *new = new_atom(DATA,align);
new->content.db = p;
return new;
}
atom *new_label_atom(symbol *p)
{
atom *new = new_atom(LABEL,1);
new->content.label = p;
return new;
}
atom *new_space_atom(expr *space,size_t size,expr *fill)
{
atom *new = new_atom(SPACE,1);
int i;
if (size<1)
ierror(0); /* usually an error in syntax-module */
new->content.sb = new_sblock(space,size,fill);
return new;
}
atom *new_datadef_atom(size_t bitsize,operand *op)
{
atom *new = new_atom(DATADEF,DATA_ALIGN(bitsize));
new->content.defb = mymalloc(sizeof(*new->content.defb));
new->content.defb->bitsize = bitsize;
new->content.defb->op = op;
return new;
}
atom *new_srcline_atom(int line)
{
atom *new = new_atom(LINE,1);
new->content.srcline = line;
return new;
}
atom *new_opts_atom(void *o)
{
atom *new = new_atom(OPTS,1);
new->content.opts = o;
return new;
}
atom *new_text_atom(char *txt)
{
atom *new = new_atom(PRINTTEXT,1);
new->content.ptext = txt ? txt : "\n";
return new;
}
atom *new_expr_atom(expr *exp,int type,int size)
{
atom *new = new_atom(PRINTEXPR,1);
new->content.pexpr = mymalloc(sizeof(*new->content.pexpr));
if (exp==NULL || type<PEXP_HEX || type>PEXP_ASC || size<1
|| size>sizeof(long long)*8)
ierror(0);
new->content.pexpr->print_exp = exp;
new->content.pexpr->type = type;
new->content.pexpr->size = size;
return new;
}
atom *new_roffs_atom(expr *offs)
{
atom *new = new_atom(ROFFS,1);
new->content.roffs = offs;
return new;
}
atom *new_rorg_atom(taddr raddr)
{
atom *new = new_atom(RORG,1);
taddr *newrorg = mymalloc(sizeof(taddr));
*newrorg = raddr;
new->content.rorg = newrorg;
return new;
}
atom *new_rorgend_atom(void)
{
return new_atom(RORGEND,1);
}
atom *new_assert_atom(expr *aexp,char *exp,char *msg)
{
atom *new = new_atom(ASSERT,1);
new->content.assert = mymalloc(sizeof(*new->content.assert));
new->content.assert->assert_exp = aexp;
new->content.assert->expstr = exp;
new->content.assert->msgstr = msg;
return new;
}
atom *new_nlist_atom(char *name,int type,int other,int desc,expr *value)
{
atom *new = new_atom(NLIST,1);
new->content.nlist = mymalloc(sizeof(*new->content.nlist));
new->content.nlist->name = name;
new->content.nlist->type = type;
new->content.nlist->other = other;
new->content.nlist->desc = desc;
new->content.nlist->value = value;
return new;
}