History

absolute symbols support - .org, .fill and .previous directives

bellard committed 22 years ago

Commit 0bd402d1 parent 96684991 file · +189 −36Browse files

Unified · Split

tccasm.c+189−36
@@ -28,6 +28,8 @@ static int asm_get_local_label_name(TCCState *s1, unsigned int n) · 27 unmodified lines
2828 return ts->tok;
2929 }
3030
31+static void asm_expr(TCCState *s1, ExprValue *pe);
32+
3133 /* We do not use the C expression parser to handle symbols. Maybe the
3234 C expression parser could be tweaked to do so. */
3335
@@ -91,6 +93,11 @@ static void asm_expr_unary(TCCState *s1, ExprValue *pe) · 57 unmodified lines
9193 pe->sym = NULL;
9294 next();
9395 break;
96+ case '(':
97+ next();
98+ asm_expr(s1, pe);
99+ skip(')');
100+ break;
94101 default:
95102 if (tok >= TOK_IDENT) {
96103 /* label case : if the label was not found, add one */
@@ -100,8 +107,14 @@ static void asm_expr_unary(TCCState *s1, ExprValue *pe) · 3 unmodified lines
100107 /* NOTE: by default, the symbol is global */
101108 sym->type.t = VT_VOID;
102109 }
103− pe->v = 0;
104− pe->sym = sym;
110+ if (sym->r == SHN_ABS) {
111+ /* if absolute symbol, no need to put a symbol value */
112+ pe->v = (long)sym->next;
113+ pe->sym = NULL;
114+ } else {
115+ pe->v = 0;
116+ pe->sym = sym;
117+ }
105118 next();
106119 } else {
107120 error("bad expression syntax [%s]", get_tok_str(tok, &tokc));
@@ -241,7 +254,8 @@ static int asm_int_expr(TCCState *s1) · 133 unmodified lines
241254
242255 /* NOTE: the same name space as C labels is used to avoid using too
243256 much memory when storing labels in TokenStrings */
244−static void asm_new_label(TCCState *s1, int label, int is_local)
257+static void asm_new_label1(TCCState *s1, int label, int is_local,
258+ int sh_num, int value)
245259 {
246260 Sym *sym;
247261
@@ -262,18 +276,29 @@ static void asm_new_label(TCCState *s1, int label, int is_local) · 14 unmodified lines
262276 sym = label_push(&s1->asm_labels, label, 0);
263277 sym->type.t = VT_STATIC | VT_VOID;
264278 }
265− sym->r = cur_text_section->sh_num;
266− sym->next = (void *)ind;
279+ sym->r = sh_num;
280+ sym->next = (void *)value;
281+}
282+
283+static void asm_new_label(TCCState *s1, int label, int is_local)
284+{
285+ asm_new_label1(s1, label, is_local, cur_text_section->sh_num, ind);
267286 }
268287
269288 static void asm_free_labels(TCCState *st)
270289 {
271290 Sym *s, *s1;
291+ Section *sec;
292+
272293 for(s = st->asm_labels; s != NULL; s = s1) {
273294 s1 = s->prev;
274295 /* define symbol value in object file */
275296 if (s->r) {
276− put_extern_sym(s, st->sections[s->r], (long)s->next, 0);
297+ if (s->r == SHN_ABS)
298+ sec = SECTION_ABS;
299+ else
300+ sec = st->sections[s->r];
301+ put_extern_sym(s, sec, (long)s->next, 0);
277302 }
278303 /* remove label */
279304 table_ident[s->v - TOK_IDENT]->sym_label = NULL;
@@ -282,15 +307,20 @@ static void asm_free_labels(TCCState *st) · 2 unmodified lines
282307 st->asm_labels = NULL;
283308 }
284309
285−static void use_section(TCCState *s1, const char *name)
310+static void use_section1(TCCState *s1, Section *sec)
286311 {
287− Section *sec;
288− sec = find_section(s1, name);
289312 cur_text_section->data_offset = ind;
290313 cur_text_section = sec;
291314 ind = cur_text_section->data_offset;
292315 }
293316
317+static void use_section(TCCState *s1, const char *name)
318+{
319+ Section *sec;
320+ sec = find_section(s1, name);
321+ use_section1(s1, sec);
322+}
323+
294324 static void asm_parse_directive(TCCState *s1)
295325 {
296326 int n, offset, v, size, tok1;
@@ -312,6 +342,9 @@ static void asm_parse_directive(TCCState *s1) · 15 unmodified lines
312342 error("alignment must be a positive power of two");
313343 offset = (ind + n - 1) & -n;
314344 size = offset - ind;
345+ /* the section must have a compatible alignment */
346+ if (sec->sh_addralign < n)
347+ sec->sh_addralign = n;
315348 } else {
316349 size = n;
317350 }
@@ -320,6 +353,7 @@ static void asm_parse_directive(TCCState *s1) · 2 unmodified lines
320353 next();
321354 v = asm_int_expr(s1);
322355 }
356+ zero_pad:
323357 if (sec->sh_type != SHT_NOBITS) {
324358 sec->data_offset = ind;
325359 ptr = section_ptr_add(sec, size);
@@ -361,6 +395,61 @@ static void asm_parse_directive(TCCState *s1) · 35 unmodified lines
361395 next();
362396 }
363397 break;
398+ case TOK_ASM_fill:
399+ {
400+ int repeat, size, val, i, j;
401+ uint8_t repeat_buf[8];
402+ next();
403+ repeat = asm_int_expr(s1);
404+ if (repeat < 0) {
405+ error("repeat < 0; .fill ignored");
406+ break;
407+ }
408+ size = 1;
409+ val = 0;
410+ if (tok == ',') {
411+ next();
412+ size = asm_int_expr(s1);
413+ if (size < 0) {
414+ error("size < 0; .fill ignored");
415+ break;
416+ }
417+ if (size > 8)
418+ size = 8;
419+ if (tok == ',') {
420+ next();
421+ val = asm_int_expr(s1);
422+ }
423+ }
424+ /* XXX: endianness */
425+ repeat_buf[0] = val;
426+ repeat_buf[1] = val >> 8;
427+ repeat_buf[2] = val >> 16;
428+ repeat_buf[3] = val >> 24;
429+ repeat_buf[4] = 0;
430+ repeat_buf[5] = 0;
431+ repeat_buf[6] = 0;
432+ repeat_buf[7] = 0;
433+ for(i = 0; i < repeat; i++) {
434+ for(j = 0; j < size; j++) {
435+ g(repeat_buf[j]);
436+ }
437+ }
438+ }
439+ break;
440+ case TOK_ASM_org:
441+ {
442+ unsigned long n;
443+ next();
444+ /* XXX: handle section symbols too */
445+ n = asm_int_expr(s1);
446+ if (n < ind)
447+ error("attempt to .org backwards");
448+ v = 0;
449+ size = n - ind;
450+ goto zero_pad;
451+ }
452+ break;
364453 case TOK_ASM_globl:
365454 case TOK_ASM_global:
366455 {
@@ -420,9 +509,28 @@ static void asm_parse_directive(TCCState *s1) · 53 unmodified lines
420509 pstrcat(sname, sizeof(sname), get_tok_str(tok, NULL));
421510 next();
422511 }
512+ if (tok == ',') {
513+ /* skip section options */
514+ next();
515+ if (tok != TOK_STR)
516+ expect("string constant");
517+ next();
518+ }
519+ last_text_section = cur_text_section;
423520 use_section(s1, sname);
424521 }
425522 break;
523+ case TOK_ASM_previous:
524+ {
525+ Section *sec;
526+ next();
527+ if (!last_text_section)
528+ error("no previous section referenced");
529+ sec = cur_text_section;
530+ use_section1(s1, last_text_section);
531+ last_text_section = sec;
532+ }
533+ break;
426534 default:
427535 error("unknown assembler directive '.%s'", get_tok_str(tok, NULL));
428536 break;
@@ -471,7 +579,7 @@ static int tcc_assemble_internal(TCCState *s1, int do_preprocess) · 42 unmodified lines
471579
472580 ch = file->buf_ptr[0];
473581 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
474− parse_flags = 0;
582+ parse_flags = PARSE_FLAG_ASM_COMMENTS;
475583 if (do_preprocess)
476584 parse_flags |= PARSE_FLAG_PREPROCESS;
477585 next();
@@ -507,6 +615,12 @@ static int tcc_assemble_internal(TCCState *s1, int do_preprocess) · 29 unmodified lines
507615 asm_new_label(s1, opcode, 0);
508616 next();
509617 goto redo;
618+ } else if (tok == '=') {
619+ int n;
620+ next();
621+ n = asm_int_expr(s1);
622+ asm_new_label1(s1, opcode, 0, SHN_ABS, n);
623+ goto redo;
510624 } else {
511625 asm_opcode(s1, opcode);
512626 }
@@ -527,6 +641,7 @@ static int tcc_assemble_internal(TCCState *s1, int do_preprocess) · 14 unmodified lines
527641 /* Assemble the current file */
528642 static int tcc_assemble(TCCState *s1, int do_preprocess)
529643 {
644+ Sym *define_start;
530645 int ret;
531646
532647 preprocess_init(s1);
@@ -535,9 +650,14 @@ static int tcc_assemble(TCCState *s1, int do_preprocess) · 2 unmodified lines
535650 cur_text_section = text_section;
536651 ind = cur_text_section->data_offset;
537652
653+ define_start = define_stack;
654+
538655 ret = tcc_assemble_internal(s1, do_preprocess);
539656
540657 cur_text_section->data_offset = ind;
658+
659+ free_defines(define_start);
660+
541661 return ret;
542662 }
543663
@@ -691,12 +811,14 @@ static void parse_asm_operands(ASMOperand *operands, int *nb_operands_ptr, · 147 unmodified lines
691811 if (is_output) {
692812 test_lvalue();
693813 } else {
694− /* we want to avoid LLOCAL case. note that it may come
695− from register storage, so we need to convert (reg)
814+ /* we want to avoid LLOCAL case, except when the 'm'
815+ constraint is used. Note that it may come from
816+ register storage, so we need to convert (reg)
696817 case */
697818 if ((vtop->r & VT_LVAL) &&
698819 ((vtop->r & VT_VALMASK) == VT_LLOCAL ||
699− (vtop->r & VT_VALMASK) < VT_CONST)) {
820+ (vtop->r & VT_VALMASK) < VT_CONST) &&
821+ !strchr(op->constraint, 'm')) {
700822 gv(RC_INT);
701823 }
702824 }
@@ -712,14 +834,27 @@ static void parse_asm_operands(ASMOperand *operands, int *nb_operands_ptr, · 9 unmodified lines
712834 }
713835 }
714836
837+static void parse_asm_str(CString *astr)
838+{
839+ skip('(');
840+ /* read the string */
841+ if (tok != TOK_STR)
842+ expect("string constant");
843+ cstr_new(astr);
844+ while (tok == TOK_STR) {
845+ /* XXX: add \0 handling too ? */
846+ cstr_cat(astr, tokc.cstr->data);
847+ next();
848+ }
849+ cstr_ccat(astr, '\0');
850+}
851+
715852 /* parse the GCC asm() instruction */
716853 static void asm_instr(void)
717854 {
718855 CString astr, astr1;
719856 ASMOperand operands[MAX_ASM_OPERANDS];
720− int nb_inputs, nb_outputs, nb_operands, i;
721− uint8_t input_regs_allocated[NB_ASM_REGS];
722− uint8_t output_regs_allocated[NB_ASM_REGS];
857+ int nb_inputs, nb_outputs, nb_operands, i, must_subst, out_reg;
723858 uint8_t clobber_regs[NB_ASM_REGS];
724859
725860 next();
@@ -728,22 +863,14 @@ static void asm_instr(void) · 2 unmodified lines
728863 if (tok == TOK_VOLATILE1 || tok == TOK_VOLATILE2 || tok == TOK_VOLATILE3) {
729864 next();
730865 }
731− skip('(');
732− /* read the string */
733− if (tok != TOK_STR)
734− expect("string constant");
735− cstr_new(&astr);
736− while (tok == TOK_STR) {
737− /* XXX: add \0 handling too ? */
738− cstr_cat(&astr, tokc.cstr->data);
739− next();
740− }
741− cstr_ccat(&astr, '\0');
866+ parse_asm_str(&astr);
742867 nb_operands = 0;
743868 nb_outputs = 0;
869+ must_subst = 0;
744870 memset(clobber_regs, 0, sizeof(clobber_regs));
745871 if (tok == ':') {
746872 next();
873+ must_subst = 1;
747874 /* output args */
748875 parse_asm_operands(operands, &nb_operands, 1);
749876 nb_outputs = nb_operands;
@@ -780,19 +907,15 @@ static void asm_instr(void) · 30 unmodified lines
780907 save_regs(0);
781908
782909 /* compute constraints */
783− asm_compute_constraints(input_regs_allocated,
784− operands, nb_operands, nb_outputs, 0,
785− NULL);
786− asm_compute_constraints(output_regs_allocated,
787− operands, nb_operands, nb_outputs, 1,
788− input_regs_allocated);
910+ asm_compute_constraints(operands, nb_operands, nb_outputs,
911+ clobber_regs, &out_reg);
789912
790913 /* substitute the operands in the asm string. No substitution is
791914 done if no operands (GCC behaviour) */
792915 #ifdef ASM_DEBUG
793916 printf("asm: \"%s\"\n", (char *)astr.data);
794917 #endif
795− if (nb_operands > 0) {
918+ if (must_subst) {
796919 subst_asm_operands(operands, nb_operands, nb_outputs, &astr1, &astr);
797920 cstr_free(&astr);
798921 } else {
@@ -803,7 +926,8 @@ static void asm_instr(void) · 4 unmodified lines
803926 #endif
804927
805928 /* generate loads */
806− asm_gen_code(operands, nb_operands, nb_outputs, 0, clobber_regs);
929+ asm_gen_code(operands, nb_operands, nb_outputs, 0,
930+ clobber_regs, out_reg);
807931
808932 /* assemble the string with tcc internal assembler */
809933 tcc_assemble_inline(tcc_state, astr1.data, astr1.size - 1);
@@ -812,7 +936,8 @@ static void asm_instr(void) · 2 unmodified lines
812936 next();
813937
814938 /* store the output values if needed */
815− asm_gen_code(operands, nb_operands, nb_outputs, 1, clobber_regs);
939+ asm_gen_code(operands, nb_operands, nb_outputs, 1,
940+ clobber_regs, out_reg);
816941
817942 /* free everything */
818943 for(i=0;i<nb_operands;i++) {
@@ -824,3 +949,31 @@ static void asm_instr(void) · 5 unmodified lines
824949 cstr_free(&astr1);
825950 }
826951
952+static void asm_global_instr(void)
953+{
954+ CString astr;
955+
956+ next();
957+ parse_asm_str(&astr);
958+ skip(')');
959+ /* NOTE: we do not eat the ';' so that we can restore the current
960+ token after the assembler parsing */
961+ if (tok != ';')
962+ expect("';'");
963+
964+#ifdef ASM_DEBUG
965+ printf("asm_global: \"%s\"\n", (char *)astr.data);
966+#endif
967+ cur_text_section = text_section;
968+ ind = cur_text_section->data_offset;
969+
970+ /* assemble the string with tcc internal assembler */
971+ tcc_assemble_inline(tcc_state, astr.data, astr.size - 1);
972+
973+ cur_text_section->data_offset = ind;
974+
975+ /* restore the current C token */
976+ next();
977+
978+ cstr_free(&astr);
979+}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t