History

arm-asm: Add ldr, ldrb, str, strb

Danny Milosavljevic committed 5 years ago

Commit c7682dd1 parent 632b2132 files · +128 −0Browse files

Unified · Split

2 files
  1. arm-asm.c +123 −0
  2. arm-tok.h +5 −0
arm-asm.c+123−0
@@ -506,6 +506,123 @@ static void asm_long_multiplication_opcode(TCCState *s1, int token) · 505 unmodified lines
506506 }
507507 }
508508
509+static void asm_single_data_transfer_opcode(TCCState *s1, int token)
510+{
511+ Operand ops[3];
512+ int exclam = 0;
513+ int closed_bracket = 0;
514+ int op2_minus = 0;
515+ uint32_t opcode = 1 << 26;
516+ // Note: ldr r0, [r4, #4] ; simple offset: r0 = *(int*)(r4+4); r4 unchanged
517+ // Note: ldr r0, [r4, #4]! ; pre-indexed: r0 = *(int*)(r4+4); r4 = r4+4
518+ // Note: ldr r0, [r4], #4 ; post-indexed: r0 = *(int*)(r4+0); r4 = r4+4
519+
520+ parse_operand(s1, &ops[0]);
521+ if (ops[0].type == OP_REG32)
522+ opcode |= ENCODE_RD(ops[0].reg);
523+ else {
524+ expect("(destination operand) register");
525+ return;
526+ }
527+ if (tok != ',')
528+ expect("two arguments");
529+ else
530+ next(); // skip ','
531+ if (tok != '[')
532+ expect("'['");
533+ else
534+ next(); // skip '['
535+
536+ parse_operand(s1, &ops[1]);
537+ if (ops[1].type == OP_REG32)
538+ opcode |= ENCODE_RN(ops[1].reg);
539+ else {
540+ expect("(first source operand) register");
541+ return;
542+ }
543+ if (tok == ']') {
544+ next();
545+ closed_bracket = 1;
546+ // exclam = 1; // implicit in hardware; don't do it in software
547+ }
548+ if (tok != ',')
549+ expect("','");
550+ else
551+ next(); // skip ','
552+ if (tok == '-') {
553+ op2_minus = 1;
554+ next();
555+ }
556+ parse_operand(s1, &ops[2]);
557+ if (!closed_bracket) {
558+ if (tok != ']')
559+ expect("']'");
560+ else
561+ next(); // skip ']'
562+ opcode |= 1 << 24; // add offset before transfer
563+ if (tok == '!') {
564+ exclam = 1;
565+ next(); // skip '!'
566+ }
567+ }
568+
569+ // single data transfer: 0 1 I P U B W L << 20 (general case):
570+ // operands:
571+ // Rd: destination operand [ok]
572+ // Rn: first source operand [ok]
573+ // Operand2: bits 11...0 [ok]
574+ // I: immediate operand? [ok]
575+ // P: Pre/post indexing is PRE: Add offset before transfer [ok]
576+ // U: Up/down is up? (*adds* offset to base) [ok]
577+ // B: Byte/word is byte? TODO
578+ // W: Write address back into base? [ok]
579+ // L: Load/store is load? [ok]
580+ if (exclam)
581+ opcode |= 1 << 21; // write offset back into register
582+
583+ if (ops[2].type == OP_IM32 || ops[2].type == OP_IM8 || ops[2].type == OP_IM8N) {
584+ int v = ops[2].e.v;
585+ if (op2_minus)
586+ tcc_error("minus before '#' not supported for immediate values");
587+ if (v >= 0) {
588+ opcode |= 1 << 23; // up
589+ if (v >= 0x1000)
590+ tcc_error("offset out of range for '%s'", get_tok_str(token, NULL));
591+ else
592+ opcode |= v;
593+ } else { // down
594+ if (v <= -0x1000)
595+ tcc_error("offset out of range for '%s'", get_tok_str(token, NULL));
596+ else
597+ opcode |= -v;
598+ }
599+ } else if (ops[2].type == OP_REG32) {
600+ if (!op2_minus)
601+ opcode |= 1 << 23; // up
602+ opcode |= ENCODE_IMMEDIATE_FLAG; /* if set, it means it's NOT immediate */
603+ opcode |= ops[2].reg;
604+ } else
605+ expect("register");
606+
607+ switch (ARM_INSTRUCTION_GROUP(token)) {
608+ case TOK_ASM_strbeq:
609+ opcode |= 1 << 22; // B
610+ /* fallthrough */
611+ case TOK_ASM_streq:
612+ asm_emit_opcode(token, opcode);
613+ break;
614+ case TOK_ASM_ldrbeq:
615+ opcode |= 1 << 22; // B
616+ /* fallthrough */
617+ case TOK_ASM_ldreq:
618+ opcode |= 1 << 20; // L
619+ asm_emit_opcode(token, opcode);
620+ break;
621+ default:
622+ expect("data transfer instruction");
623+ }
624+}
625+
509626 ST_FUNC void asm_opcode(TCCState *s1, int token)
510627 {
511628 while (token == TOK_LINEFEED) {
@@ -546,6 +663,12 @@ ST_FUNC void asm_opcode(TCCState *s1, int token) · 34 unmodified lines
546663 case TOK_ASM_uxtheq:
547664 return asm_binary_opcode(s1, token);
548665
666+ case TOK_ASM_ldreq:
667+ case TOK_ASM_ldrbeq:
668+ case TOK_ASM_streq:
669+ case TOK_ASM_strbeq:
670+ return asm_single_data_transfer_opcode(s1, token);
671+
549672 case TOK_ASM_muleq:
550673 case TOK_ASM_mulseq:
551674 case TOK_ASM_mlaeq:
arm-tok.h+5−0
@@ -83,6 +83,11 @@ · 82 unmodified lines
8383
8484 /* load/store */
8585
86+ DEF_ASM_CONDED(ldr)
87+ DEF_ASM_CONDED(ldrb)
88+ DEF_ASM_CONDED(str)
89+ DEF_ASM_CONDED(strb)
90+
8691 DEF_ASM_CONDED(stmda)
8792 DEF_ASM_CONDED(ldmda)
8893 DEF_ASM_CONDED(stm)

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