-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharm-thumb-asm.c
More file actions
3551 lines (3308 loc) · 92.3 KB
/
arm-thumb-asm.c
File metadata and controls
3551 lines (3308 loc) · 92.3 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
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
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* ARMvX-m assembly generator for TCC
* Uses thumb instruction set
*
* Based on
* ARM specific functions for TCC assembler
* Copyright (c) 2001, 2002 Fabrice Bellard
* Copyright (c) 2020 Danny Milosavljevic
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#define USING_GLOBALS
#include <ctype.h>
#include <string.h>
#include "arm-thumb-opcodes.h"
#include "tcc.h"
#include "tccir.h"
/* Forward declarations for MOP-based load/store from arm-thumb-gen.c */
void tcc_gen_mach_load_to_reg(int dest_reg, const MachineOperand *op);
void tcc_gen_mach_store_from_reg(int src_reg, const MachineOperand *op);
enum
{
OPT_REG32,
OPT_REGSET32,
OPT_IM8,
OPT_IM8N,
OPT_IM32,
OPT_VREG32,
OPT_VREG64,
};
#define OP_REG32 (1 << OPT_REG32)
#define OP_VREG32 (1 << OPT_VREG32)
#define OP_VREG64 (1 << OPT_VREG64)
#define OP_REG (OP_REG32 | OP_VREG32 | OP_VREG64)
#define OP_IM32 (1 << OPT_IM32)
#define OP_IM8 (1 << OPT_IM8)
#define OP_IM8N (1 << OPT_IM8N)
#define OP_REGSET32 (1 << OPT_REGSET32)
#define OP_VREGSETS32 (OP_VREG32 | OP_REGSET32)
#define OP_VREGSETD32 (OP_VREG64 | OP_REGSET32)
static bool thumb_operand_is_immediate(int type)
{
if (type != OP_IM32 && type != OP_IM8 && type != OP_IM8N)
{
return false;
}
return true;
}
static bool thumb_operand_is_register(int type)
{
if (type != OP_REG && type != OP_REG32)
{
return false;
}
return true;
}
static bool thumb_operand_is_registerset(int type)
{
if (type != OP_REGSET32)
{
return false;
}
return true;
}
typedef struct Operand
{
uint32_t type;
union
{
uint8_t reg;
uint32_t regset;
ExprValue e;
};
} Operand;
ST_FUNC void g(int c)
{
int ind1;
if (nocode_wanted)
return;
/* During dry-run, don't write to section data, just track position */
if (tcc_gen_machine_dry_run_is_active())
{
ind++;
return;
}
ind1 = ind + 1;
if (ind1 > cur_text_section->data_allocated)
section_realloc(cur_text_section, ind1);
cur_text_section->data[ind] = c;
ind = ind1;
}
ST_FUNC void gen_le16(int i)
{
g(i);
g(i >> 8);
}
ST_FUNC void gen_le32(int i)
{
int ind1;
if (nocode_wanted)
return;
/* During dry-run, don't write to section data, just track position */
if (tcc_gen_machine_dry_run_is_active())
{
ind += 4;
return;
}
ind1 = ind + 4;
if (ind1 > cur_text_section->data_allocated)
section_realloc(cur_text_section, ind1);
cur_text_section->data[ind++] = i & 0xFF;
cur_text_section->data[ind++] = (i >> 8) & 0xFF;
cur_text_section->data[ind++] = (i >> 16) & 0xFF;
cur_text_section->data[ind++] = (i >> 24) & 0xFF;
}
ST_FUNC void gen_expr32(ExprValue *pe)
{
if (pe->sym)
{
/* Emit relocation for symbol reference */
greloca(cur_text_section, pe->sym, ind, R_ARM_ABS32, pe->v);
gen_le32(0); /* Placeholder, will be filled by relocation */
}
else
{
gen_le32(pe->v);
}
}
int is_valid_opcode(thumb_opcode op);
static void thumb_emit_opcode(thumb_opcode op)
{
if (!is_valid_opcode(op))
{
tcc_error("compiler_error: received invalid opcode: 0x%x\n", op.opcode);
}
if (op.size == 4)
{
gen_le16(op.opcode >> 16);
}
gen_le16(op.opcode & 0xffff);
}
ST_FUNC void subst_asm_operand(CString *add_str, SValue *sv, int modifier)
{
int r, reg, size, val;
r = sv->r;
if ((r & VT_VALMASK) == VT_CONST)
{
if (!(r & VT_LVAL) && modifier != 'c' && modifier != 'n' && modifier != 'P')
cstr_ccat(add_str, '#');
if (r & VT_SYM)
{
const char *name = get_tok_str(sv->sym->v, NULL);
if (sv->sym->v >= SYM_FIRST_ANOM)
{
/* In case of anonymous symbols ("L.42", used
for static data labels) we can't find them
in the C symbol table when later looking up
this name. So enter them now into the asm label
list when we still know the symbol. */
get_asm_sym(tok_alloc(name, strlen(name))->tok, sv->sym);
}
if (tcc_state->leading_underscore)
cstr_ccat(add_str, '_');
cstr_cat(add_str, name, -1);
if ((uint32_t)sv->c.i == 0)
goto no_offset;
cstr_ccat(add_str, '+');
}
val = sv->c.i;
if (modifier == 'n')
val = -val;
cstr_printf(add_str, "%d", (int)sv->c.i);
no_offset:;
}
else if ((r & VT_VALMASK) == VT_LOCAL)
{
cstr_printf(add_str, "[fp,#%d]", (int)sv->c.i);
}
else if (r & VT_LVAL)
{
reg = r & VT_VALMASK;
if (reg >= VT_CONST)
tcc_internal_error("");
cstr_printf(add_str, "[%s]", get_tok_str(TOK_ASM_r0 + reg, NULL));
}
else
{
/* register case */
reg = r & VT_VALMASK;
if (reg >= VT_CONST)
tcc_internal_error("");
/* choose register operand size */
if ((sv->type.t & VT_BTYPE) == VT_BYTE || (sv->type.t & VT_BTYPE) == VT_BOOL)
size = 1;
else if ((sv->type.t & VT_BTYPE) == VT_SHORT)
size = 2;
else
size = 4;
if (modifier == 'b')
{
size = 1;
}
else if (modifier == 'w')
{
size = 2;
}
else if (modifier == 'k')
{
size = 4;
}
switch (size)
{
default:
reg = TOK_ASM_r0 + reg;
break;
}
cstr_printf(add_str, "%s", get_tok_str(reg, NULL));
}
}
/* generate prolog and epilog code for asm statement */
ST_FUNC void asm_gen_code(ASMOperand *operands, int nb_operands, int nb_outputs, int is_output, uint8_t *clobber_regs,
int out_reg)
{
uint8_t regs_allocated[NB_ASM_REGS];
ASMOperand *op;
int i, reg;
uint32_t saved_regset = 0;
// TODO: Check non-E ABI.
// Note: Technically, r13 (sp) is also callee-saved--but that does not matter
// yet
static const uint8_t reg_saved[] = {4, 5, 6, 7, 8, 9 /* Note: sometimes special reg "sb" */, 10, 11};
/* mark all used registers */
memcpy(regs_allocated, clobber_regs, sizeof(regs_allocated));
for (i = 0; i < nb_operands; i++)
{
op = &operands[i];
if (op->reg >= 0)
regs_allocated[op->reg] = 1;
}
for (i = 0; i < sizeof(reg_saved) / sizeof(reg_saved[0]); i++)
{
reg = reg_saved[i];
if (regs_allocated[reg])
saved_regset |= 1 << reg;
}
if (!is_output)
{ // prolog
/* generate reg save code */
if (saved_regset)
{
gen_le16(0xe92d); /* STMDB SP!, first halfword */
gen_le16(saved_regset); /* register list second halfword */
}
/* generate load code */
for (i = 0; i < nb_operands; i++)
{
op = &operands[i];
if (op->reg >= 0)
{
if ((op->vt->r & VT_VALMASK) == VT_LLOCAL && op->is_memory)
{
/* memory reference case (for both input and
output cases) */
/* Convert LLOCAL stack slot to a pointer in a LOCAL stack slot.
This matches the old SValue rewrite to VT_LOCAL|VT_LVAL with VT_PTR type. */
IROperand src = svalue_to_iroperand(tcc_state->ir, op->vt);
src.is_llocal = 0;
src.is_lval = 1;
src.btype = IROP_BTYPE_INT32; /* pointers are 32-bit on ARMv8-M */
MachineOperand mop = machine_op_from_ir(tcc_state->ir, &src);
tcc_gen_mach_load_to_reg(op->reg, &mop);
}
else if (i >= nb_outputs || op->is_rw)
{ // not write-only
/* load value in register */
IROperand src = svalue_to_iroperand(tcc_state->ir, op->vt);
MachineOperand mop = machine_op_from_ir(tcc_state->ir, &src);
tcc_gen_mach_load_to_reg(op->reg, &mop);
if (op->is_llong)
tcc_error("long long not implemented");
}
}
}
}
else
{ // epilog
/* generate save code */
for (i = 0; i < nb_outputs; i++)
{
op = &operands[i];
if (op->reg >= 0)
{
if ((op->vt->r & VT_VALMASK) == VT_LLOCAL)
{
if (!op->is_memory)
{
IROperand ir_op = svalue_to_iroperand(tcc_state->ir, op->vt);
/* Load pointer from LOCAL stack slot into out_reg.
Change LLOCAL->LOCAL and set btype to PTR (INT32). */
IROperand addr = ir_op;
addr.is_llocal = 0;
addr.btype = IROP_BTYPE_INT32;
MachineOperand addr_mop = machine_op_from_ir(tcc_state->ir, &addr);
tcc_gen_mach_load_to_reg(out_reg, &addr_mop);
/* Store op->reg through the pointer now in out_reg */
MachineOperand store_mop;
memset(&store_mop, 0, sizeof(store_mop));
store_mop.kind = MACH_OP_REG;
store_mop.btype = irop_get_btype(ir_op);
store_mop.is_unsigned = ir_op.is_unsigned;
store_mop.u.reg.r0 = out_reg;
store_mop.u.reg.r1 = -1;
store_mop.needs_deref = true;
tcc_gen_mach_store_from_reg(op->reg, &store_mop);
}
}
else
{
IROperand ir_op = svalue_to_iroperand(tcc_state->ir, op->vt);
MachineOperand mop = machine_op_from_ir(tcc_state->ir, &ir_op);
tcc_gen_mach_store_from_reg(op->reg, &mop);
if (op->is_llong)
tcc_error("long long not implemented");
}
}
}
/* generate reg restore code */
if (saved_regset)
{
gen_le16(0xe8bd); /* LDMIA SP!, first halfword */
gen_le16(saved_regset); /* register list second halfword */
}
}
}
/* return the constraint priority (we allocate first the lowest
numbered constraints) */
static inline int constraint_priority(const char *str)
{
int priority, c, pr;
/* we take the lowest priority */
priority = 0;
for (;;)
{
c = *str;
if (c == '\0')
break;
str++;
switch (c)
{
case ',':
continue;
case 'l': // in ARM mode, that's an alias for 'r' [ARM].
case 'r': // register [general]
case 'p': // valid memory address for load,store [general]
pr = 3;
break;
case 'M': // integer constant for shifts [ARM]
case 'I': // integer valid for data processing instruction immediate
case 'J': // integer in range -4095...4095
case 'n': // immediate integer operand with a known numeric value
case 'i': // immediate integer operand, including symbolic constants
case 's': // immediate integer operand whose value is not an explicit integer
// [general]
case 'Q': // memory reference with a single base register [ARM]
case 'm': // memory operand [general]
case 'g': // general-purpose-register, memory, immediate integer [general]
case 'X': // any operand whatsoever [general]
pr = 4;
break;
default:
tcc_error("unknown constraint '%c'", c);
}
if (pr > priority)
priority = pr;
}
return priority;
}
static const char *skip_constraint_modifiers(const char *p)
{
/* Constraint modifier:
= Operand is written to by this instruction
+ Operand is both read and written to by this instruction
% Instruction is commutative for this operand and the following operand.
Per-alternative constraint modifier:
& Operand is clobbered before the instruction is done using the input
operands
*/
while (*p == '=' || *p == '&' || *p == '+' || *p == '%')
p++;
return p;
}
#define REG_OUT_MASK 0x01
#define REG_IN_MASK 0x02
#define is_reg_allocated(reg) (regs_allocated[reg] & reg_mask)
ST_FUNC void asm_compute_constraints(ASMOperand *operands, int nb_operands, int nb_outputs, const uint8_t *clobber_regs,
const uint8_t *reserved_regs, int *pout_reg)
{
/* overall format: modifier, then ,-seperated list of alternatives; all
* operands for a single instruction must have the same number of alternatives
*/
/* TODO: Simple constraints
whitespace ignored
o memory operand that is offsetable
V memory but not offsetable
< memory operand with autodecrement addressing is allowed. Restrictions
apply. > memory operand with autoincrement addressing is allowed.
Restrictions apply. n immediate integer operand with a known numeric value
E immediate floating operand (const_double) is allowed, but only if
target=host F immediate floating operand (const_double or const_vector) is
allowed s immediate integer operand whose value is not an explicit integer
X any operand whatsoever
0...9 (postfix); (can also be more than 1 digit number); an operand that
matches the specified operand number is allowed
*/
/* TODO: ARM constraints:
k the stack pointer register
G the floating-point constant 0.0
Q memory reference where the exact address is in a single register ("m" is
preferable for asm statements) R an item in the constant pool S symbol in the
text segment of the current file [ Uv memory reference suitable for VFP
load/store insns (reg+constant offset)] [ Uy memory reference suitable for
iWMMXt load/store instructions] Uq memory reference suitable for the ARMv4 ldrsb
instruction
*/
ASMOperand *op;
int sorted_op[MAX_ASM_OPERANDS];
int i, j, k, p1, p2, tmp, reg, c, reg_mask;
const char *str;
uint8_t regs_allocated[NB_ASM_REGS];
/* init fields */
for (i = 0; i < nb_operands; i++)
{
op = &operands[i];
op->input_index = -1;
op->ref_index = -1;
op->reg = -1;
op->is_memory = 0;
op->is_rw = 0;
}
/* compute constraint priority and evaluate references to output
constraints if input constraints */
for (i = 0; i < nb_operands; i++)
{
op = &operands[i];
str = op->constraint;
str = skip_constraint_modifiers(str);
if (isnum(*str) || *str == '[')
{
/* this is a reference to another constraint */
k = find_constraint(operands, nb_operands, str, NULL);
if ((unsigned)k >= i || i < nb_outputs)
tcc_error("invalid reference in constraint %d ('%s')", i, str);
op->ref_index = k;
if (operands[k].input_index >= 0)
tcc_error("cannot reference twice the same operand");
operands[k].input_index = i;
op->priority = 5;
}
else if ((op->vt->r & VT_VALMASK) == VT_LOCAL && op->vt->sym && (reg = op->vt->sym->r & VT_VALMASK) < VT_CONST)
{
op->priority = 1;
op->reg = reg;
}
else
{
op->priority = constraint_priority(str);
}
}
/* sort operands according to their priority */
for (i = 0; i < nb_operands; i++)
sorted_op[i] = i;
for (i = 0; i < nb_operands - 1; i++)
{
for (j = i + 1; j < nb_operands; j++)
{
p1 = operands[sorted_op[i]].priority;
p2 = operands[sorted_op[j]].priority;
if (p2 < p1)
{
tmp = sorted_op[i];
sorted_op[i] = sorted_op[j];
sorted_op[j] = tmp;
}
}
}
for (i = 0; i < NB_ASM_REGS; i++)
{
if (clobber_regs[i])
regs_allocated[i] = REG_IN_MASK | REG_OUT_MASK;
else
regs_allocated[i] = 0;
}
/* Also mark registers reserved by the IR register allocator (live variables).
* These are NOT clobbered (no save/restore in asm_gen_code), but should not be
* picked by the constraint solver for "r" operand allocation. */
if (reserved_regs)
{
for (i = 0; i < NB_ASM_REGS; i++)
{
if (reserved_regs[i])
regs_allocated[i] |= REG_IN_MASK | REG_OUT_MASK;
}
}
/* sp cannot be used */
regs_allocated[13] = REG_IN_MASK | REG_OUT_MASK;
/* fp cannot be used yet */
regs_allocated[11] = REG_IN_MASK | REG_OUT_MASK;
/* allocate registers and generate corresponding asm moves */
for (i = 0; i < nb_operands; i++)
{
j = sorted_op[i];
op = &operands[j];
str = op->constraint;
/* no need to allocate references */
if (op->ref_index >= 0)
continue;
/* select if register is used for output, input or both */
if (op->input_index >= 0)
{
reg_mask = REG_IN_MASK | REG_OUT_MASK;
}
else if (j < nb_outputs)
{
reg_mask = REG_OUT_MASK;
}
else
{
reg_mask = REG_IN_MASK;
}
if (op->reg >= 0)
{
if (is_reg_allocated(op->reg))
tcc_error("asm regvar requests register that's taken already");
reg = op->reg;
}
try_next:
c = *str++;
switch (c)
{
case ',':
goto try_next;
case '=': // Operand is written-to
goto try_next;
case '+': // Operand is both READ and written-to
op->is_rw = 1;
/* FALL THRU */
case '&': // Operand is clobbered before the instruction is done using the
// input operands
if (j >= nb_outputs)
tcc_error("'%c' modifier can only be applied to outputs", c);
reg_mask = REG_IN_MASK | REG_OUT_MASK;
goto try_next;
case 'l': // In non-thumb mode, alias for 'r'--otherwise r0-r7 [ARM]
case 'r': // general-purpose register
case 'p': // loadable/storable address
/* any general register */
if ((reg = op->reg) >= 0)
goto reg_found;
else
for (reg = 0; reg <= 8; reg++)
{
if (!is_reg_allocated(reg))
goto reg_found;
}
goto try_next;
reg_found:
/* now we can reload in the register */
op->is_llong = 0;
op->reg = reg;
regs_allocated[reg] |= reg_mask;
break;
case 'I': // integer that is valid as an data processing instruction
// immediate (0...255, rotated by a multiple of two)
case 'J': // integer in the range -4095 to 4095 [ARM]
case 'K': // integer that satisfies constraint I when inverted (one's
// complement)
case 'L': // integer that satisfies constraint I when inverted (two's
// complement)
case 'n': // immediate integer operand with a known numeric value
case 'i': // immediate integer operand, including symbolic constants
case 's': // immediate integer operand whose value is not an explicit integer
if (!((op->vt->r & (VT_VALMASK | VT_LVAL)) == VT_CONST))
goto try_next;
break;
case 'M': // integer in the range 0 to 32
if (!((op->vt->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST))
goto try_next;
break;
case 'Q': // simple memory operand [ARM]
case 'm': // memory operand
case 'g':
case 'X':
/* nothing special to do because the operand is already in
memory, except if the pointer itself is stored in a
memory variable (VT_LLOCAL case) */
/* XXX: fix constant case */
/* if it is a reference to a memory zone, it must lie
in a register, so we reserve the register in the
input registers and a load will be generated
later */
if (j < nb_outputs || c == 'm')
{
if ((op->vt->r & VT_VALMASK) == VT_LLOCAL)
{
/* any general register */
for (reg = 0; reg <= 8; reg++)
{
if (!(regs_allocated[reg] & REG_IN_MASK))
goto reg_found1;
}
goto try_next;
reg_found1:
/* now we can reload in the register */
regs_allocated[reg] |= REG_IN_MASK;
op->reg = reg;
op->is_memory = 1;
}
}
break;
default:
tcc_error("asm constraint %d ('%s') could not be satisfied", j, op->constraint);
break;
}
/* if a reference is present for that operand, we assign it too */
if (op->input_index >= 0)
{
operands[op->input_index].reg = op->reg;
operands[op->input_index].is_llong = op->is_llong;
}
}
/* compute out_reg. It is used to store outputs registers to memory
locations references by pointers (VT_LLOCAL case) */
*pout_reg = -1;
for (i = 0; i < nb_operands; i++)
{
op = &operands[i];
if (op->reg >= 0 && (op->vt->r & VT_VALMASK) == VT_LLOCAL && !op->is_memory)
{
for (reg = 0; reg <= 8; reg++)
{
if (!(regs_allocated[reg] & REG_OUT_MASK))
goto reg_found2;
}
tcc_error("could not find free output register for reloading");
reg_found2:
*pout_reg = reg;
break;
}
}
/* print sorted constraints */
#ifdef ASM_DEBUG
for (i = 0; i < nb_operands; i++)
{
j = sorted_op[i];
op = &operands[j];
printf("%%%d [%s]: \"%s\" r=0x%04x reg=%d\n", j, op->id ? get_tok_str(op->id, NULL) : "", op->constraint, op->vt->r,
op->reg);
}
if (*pout_reg >= 0)
printf("out_reg=%d\n", *pout_reg);
#endif
}
ST_FUNC void asm_clobber(uint8_t *clobber_regs, const char *str)
{
int reg;
TokenSym *ts;
if (!strcmp(str, "memory") || !strcmp(str, "cc") || !strcmp(str, "flags"))
return;
ts = tok_alloc(str, strlen(str));
reg = asm_parse_regvar(ts->tok);
if (reg == -1)
{
tcc_error("invalid clobber register '%s'", str);
}
clobber_regs[reg] = 1;
}
static int asm_parse_vfp_regvar(int t, int double_precision)
{
if (double_precision)
{
if (t >= TOK_ASM_d0 && t <= TOK_ASM_d15)
return t - TOK_ASM_d0;
}
else
{
if (t >= TOK_ASM_s0 && t <= TOK_ASM_s31)
return t - TOK_ASM_s0;
}
return -1;
}
/* If T refers to a register then return the register number and type.
Otherwise return -1. */
ST_FUNC int asm_parse_regvar(int t)
{
if (t >= TOK_ASM_r0 && t <= TOK_ASM_pc)
{ /* register name */
switch (t)
{
case TOK_ASM_fp:
return TOK_ASM_r11 - TOK_ASM_r0;
case TOK_ASM_ip:
return TOK_ASM_r12 - TOK_ASM_r0;
case TOK_ASM_sp:
return TOK_ASM_r13 - TOK_ASM_r0;
case TOK_ASM_lr:
return TOK_ASM_r14 - TOK_ASM_r0;
case TOK_ASM_pc:
return TOK_ASM_r15 - TOK_ASM_r0;
default:
return t - TOK_ASM_r0;
}
}
else if (t >= TOK_ASM_s0 && t <= TOK_ASM_s31)
{
return t - TOK_ASM_s0;
}
else if (t >= TOK_ASM_d0 && t <= TOK_ASM_d15)
{
return t - TOK_ASM_d0;
}
return -1;
}
/* Parse a text containing operand and store the result in OP */
static bool parse_operand(TCCState *s1, Operand *op)
{
ExprValue e;
int reg;
uint64_t regset = 0;
int reg_start = -1;
op->type = 0;
if (tok == TOK_ASM_rrx || tok == TOK_ASM_asl || tok == TOK_ASM_lsl || tok == TOK_ASM_asr || tok == TOK_ASM_lsr ||
tok == TOK_ASM_ror)
{
return false;
}
if (tok == '{')
{ // regset literal
int regset_type = 0;
next(); // skip '{'
while (tok != '}' && tok != TOK_EOF)
{
int new_regset = 0;
if (tok >= TOK_ASM_s0 && tok <= TOK_ASM_s31)
{
new_regset = OP_VREGSETS32;
}
else if (tok >= TOK_ASM_d0 && tok <= TOK_ASM_d15)
{
new_regset = OP_VREGSETD32;
}
else
{
new_regset = OP_REGSET32;
}
reg = asm_parse_regvar(tok);
if (reg == -1)
{
expect("register");
}
else
next(); // skip register name
if (regset_type == 0)
{
regset_type = new_regset;
}
else if (regset_type != new_regset)
{
tcc_error("mixed register types in register set");
}
if ((1 << reg) < regset)
tcc_warning("registers will be processed in ascending order by "
"hardware--but are not specified in ascending order here");
if (reg_start != -1)
{
for (int r = reg_start; r <= reg; r++)
{
regset |= 1 << r;
}
reg_start = -1;
}
else
{
regset |= 1 << reg;
}
if (tok == '-')
{
reg_start = reg;
next();
}
if (tok == ',')
next(); // skip ','
}
skip('}');
if (regset == 0)
{
// ARM instructions don't support empty regset.
tcc_error("empty register list is not supported");
}
else
{
op->type = regset_type;
op->regset = regset;
}
return true;
}
else if ((reg = asm_parse_vfp_regvar(tok, 0)) != -1)
{
next(); // skip register name
op->type = OP_VREG32;
op->reg = (uint8_t)reg;
return true;
}
else if ((reg = asm_parse_vfp_regvar(tok, 1)) != -1)
{
next(); // skip register name
op->type = OP_VREG64;
op->reg = (uint8_t)reg;
return true;
}
else if ((reg = asm_parse_regvar(tok)) != -1)
{
next(); // skip register name
op->type = OP_REG32;
op->reg = (uint8_t)reg;
return true;
}
else if (tok == '#' || tok == '$')
{
/* constant value */
next(); // skip '#' or '$'
}
asm_expr(s1, &e);
op->type = OP_IM32;
op->e = e;
if (!op->e.sym)
{
if ((int)op->e.v < 0 && (int)op->e.v >= -255)
op->type = OP_IM8N;
else if (op->e.v == (uint8_t)op->e.v)
op->type = OP_IM8;
}
else
return false;
return true;
}
static uint8_t thumb_build_it_mask(const char *pattern, uint16_t condition)
{
uint8_t mask = 0x0;
for (size_t i = 2; i < 6; ++i)
{
if (pattern[i] == 0)
{
mask |= (1 << (5 - i));
return mask;
}
if (tolower(pattern[i] == 't'))
{
mask |= (condition << (5 - i));
}
else
{
mask |= ((!condition) << (5 - i));
}
}
return mask;
}
static int thumb_conditional_scope = 0;
/* ========================================================================
* Assembly Suffix Parsing - Global state for runtime suffix parsing
* ======================================================================== */
/* Condition code name to enum mapping table - global definition */
/* Note: Must match extern declaration in arm-thumb-defs.h */
const cond_name_entry_t cond_names[] = {
{"eq", 0}, /* COND_EQ */
{"ne", 1}, /* COND_NE */
{"cs", 2}, /* COND_CS */
{"hs", 2}, /* Alias for carry set */
{"cc", 3}, /* COND_CC */
{"lo", 3}, /* Alias for carry clear */
{"mi", 4}, /* COND_MI */
{"pl", 5}, /* COND_PL */
{"vs", 6}, /* COND_VS */
{"vc", 7}, /* COND_VC */
{"hi", 8}, /* COND_HI */
{"ls", 9}, /* COND_LS */
{"ge", 10}, /* COND_GE */
{"lt", 11}, /* COND_LT */
{"gt", 12}, /* COND_GT */
{"le", 13}, /* COND_LE */
{"al", 14}, /* COND_AL */
{NULL, 14}, /* Default/unconditional terminator */
};
/* Global state for current assembly instruction suffix */
static thumb_asm_suffix current_asm_suffix __attribute__((unused)) = {
.condition = COND_AL,
.width = WIDTH_NONE,
.has_suffix = 0,
};
/* ========================================================================
* Helper macros to maintain compatibility during transition
* ======================================================================== */
#define THUMB_GET_CONDITION_FROM_STATE() (current_asm_suffix.condition)
#define THUMB_HAS_WIDE_QUALIFIER_FROM_STATE() (current_asm_suffix.width == WIDTH_WIDE)
#define THUMB_HAS_NARROW_QUALIFIER_FROM_STATE() (current_asm_suffix.width == WIDTH_NARROW)
/* ========================================================================
* Parse ARM assembly instruction suffix
* Input: token_str - full token string (e.g., "addeq.w")
* Output: suffix - parsed condition and width qualifier
* Returns: Length of suffix portion (0 if no suffix)
* ======================================================================== */
static int __attribute__((unused)) parse_asm_suffix(const char *token_str, thumb_asm_suffix *suffix)
{
const char *p = token_str;
int suffix_len = 0;
suffix->condition = COND_AL; /* Default: always */
suffix->width = WIDTH_NONE;
suffix->has_suffix = 0;
/* Skip base instruction name (it's all letters until we hit something else) */
while (*p && isalpha(*p))
p++;