Skip to content

Commit 8a6fe92

Browse files
committed
Support variable typing & integer resizing
In this patch, all variables (including IR generated variables) are now guaranteed to have default type "int" to later used for integer resizing, this includes truncation and sign extension. Currently, only following cases will generate resizing opcodes (OP_trunc & OP_sign_ext): - variable assignment to registers - function parameter preparation, variadic function parameters are default to have size of int type Additionally, several data structures and memory managements are adjusted to have extensibility, this includes local variable and syntactic block allocation. Close #166.
1 parent c13d260 commit 8a6fe92

File tree

14 files changed

+162
-94
lines changed

14 files changed

+162
-94
lines changed

src/arm-codegen.c

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,7 @@ void update_elf_offset(ph2_ir_t *ph2_ir)
126126
elf_offset += 4;
127127
return;
128128
default:
129-
printf("Unknown opcode\n");
130-
abort();
129+
fatal("Unknown opcode");
131130
}
132131
}
133132

@@ -428,24 +427,23 @@ void emit_ph2_ir(ph2_ir_t *ph2_ir)
428427
emit(__mov_i(__EQ, rd, 1));
429428
return;
430429
case OP_trunc:
431-
if (rm == 1) {
430+
if (rm == 1)
432431
rm = 0xFF;
433-
} else if (rm == 4) {
432+
else if (rm == 2)
433+
rm = 0xFFFF;
434+
else if (rm == 4)
434435
rm = 0xFFFFFFFF;
435-
} else {
436-
printf("Unsupported truncation operation with target size %d\n",
437-
rm);
438-
abort();
439-
}
436+
else
437+
fatal("Unsupported truncation operation with invalid target size");
440438

441439
emit(__and_i(__AL, rd, rn, rm));
442440
return;
443441
case OP_sign_ext:
442+
/* TODO: Allow to sign extends to other types */
444443
emit(__sxtb(__AL, rd, rn, 0));
445444
return;
446445
default:
447-
printf("Unknown opcode\n");
448-
abort();
446+
fatal("Unknown opcode");
449447
}
450448
}
451449

src/arm.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -357,10 +357,8 @@ int __teq(arm_reg rd)
357357

358358
int __sxtb(arm_cond_t cond, arm_reg rd, arm_reg rm, int rotation)
359359
{
360-
if (rotation != 0 && rotation != 8 && rotation != 16 && rotation != 24) {
361-
printf("SXTB rotation must be 0, 8, 16, or 24\n");
362-
abort();
363-
}
360+
if (rotation != 0 && rotation != 8 && rotation != 16 && rotation != 24)
361+
fatal("SXTB rotation must be 0, 8, 16, or 24");
364362

365363
return arm_encode(cond, 106, 0xF, rd,
366364
rm | ((rotation >> 3) << 10) | (0x7 << 4));

src/defs.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,6 @@ typedef struct var_list {
290290
} var_list_t;
291291

292292
struct var {
293-
char type_name[MAX_TYPE_LEN];
294293
type_t *type;
295294
char var_name[MAX_VAR_LEN];
296295
int is_ptr;

src/elf.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77

88
/* ELF file manipulation */
99

10+
#include "../config"
11+
#include "defs.h"
12+
#include "globals.c"
13+
1014
int elf_symbol_index;
1115

1216
void elf_write_str(strbuf_t *elf_array, char *vals)

src/globals.c

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -691,9 +691,7 @@ int size_var(var_t *var)
691691
if (var->is_ptr > 0 || var->is_func) {
692692
size = 4;
693693
} else {
694-
type_t *type = find_type(var->type_name, 0);
695-
if (!type)
696-
error("Incomplete type");
694+
type_t *type = var->type;
697695
if (type->size == 0)
698696
size = type->base_struct->size;
699697
else
@@ -1018,6 +1016,14 @@ void global_release()
10181016
strbuf_free(elf_section);
10191017
}
10201018

1019+
/* Reports an error without specifying a position */
1020+
void fatal(char *msg)
1021+
{
1022+
printf("[Error]: %s\n", msg);
1023+
abort();
1024+
}
1025+
1026+
/* Reports an error and specifying a position */
10211027
void error(char *msg)
10221028
{
10231029
/* Construct error source diagnostics, enabling precise identification of
@@ -1048,8 +1054,8 @@ void error(char *msg)
10481054
/* TODO: figure out the corresponding C source file path and report line
10491055
* number.
10501056
*/
1051-
printf("Error %s at source location %d\n%s\n", msg, SOURCE->size,
1052-
diagnostic);
1057+
printf("[Error]: %s\nOccurs at source location %d.\n%s\n", msg,
1058+
SOURCE->size, diagnostic);
10531059
abort();
10541060
}
10551061

@@ -1081,7 +1087,7 @@ void dump_bb_insn(func_t *func, basic_block_t *bb, bool *at_func_start)
10811087
continue;
10821088
case OP_allocat:
10831089
print_indent(1);
1084-
printf("allocat %s", rd->type_name);
1090+
printf("allocat %s", rd->type->type_name);
10851091

10861092
for (int i = 0; i < rd->is_ptr; i++)
10871093
printf("*");
@@ -1287,7 +1293,7 @@ void dump_insn()
12871293
for (func_t *func = FUNC_LIST.head; func; func = func->next) {
12881294
bool at_func_start = true;
12891295

1290-
printf("def %s", func->return_def.type_name);
1296+
printf("def %s", func->return_def.type->type_name);
12911297

12921298
for (int i = 0; i < func->return_def.is_ptr; i++)
12931299
printf("*");
@@ -1296,7 +1302,7 @@ void dump_insn()
12961302
for (int i = 0; i < func->num_params; i++) {
12971303
if (i != 0)
12981304
printf(", ");
1299-
printf("%s", func->param_defs[i].type_name);
1305+
printf("%s", func->param_defs[i].type->type_name);
13001306

13011307
for (int k = 0; k < func->param_defs[i].is_ptr; k++)
13021308
printf("*");
@@ -1312,7 +1318,7 @@ void dump_insn()
13121318
if (!bb)
13131319
continue;
13141320

1315-
if (strcmp(func->return_def.type_name, "void"))
1321+
if (func->return_def.type != TY_void)
13161322
continue;
13171323

13181324
if (bb->insn_list.tail)

0 commit comments

Comments
 (0)