Skip to content

Commit d7bc53a

Browse files
committed
Initial support for converting QBE SIL to SSA
1 parent 2a733de commit d7bc53a

File tree

7 files changed

+451
-106
lines changed

7 files changed

+451
-106
lines changed

example.ssa

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
1-
function w $strlen(w %str) {
2-
%t3 =w copy 0
3-
%i =w copy %t3
4-
@L_begin_0
5-
%t4 =w copy 1
6-
%t5 =w mul %i, %t4
7-
%t6 =w add %str, %t5
8-
%t7 =b loadb %t6
9-
jnz %t7, @L_then_0, @L_0
10-
@L_then_0
11-
%t8 =w copy 1
12-
%i =w add %i, %t8
13-
%t9 =w copy -1
14-
%t10 =w add %i, %t9
15-
@L_1
16-
jmp @L_begin_0
17-
@L_0
18-
ret %i
1+
function w $main() {
2+
%a =w copy 1
3+
%t =w alloc 4
4+
jnz %a, @L1, @L2
5+
@L1
6+
%t =w add %t, %t
7+
jmp @L3
8+
@L2
9+
%t =w copy 1
10+
@L3
11+
ret %t
1912
}

src/globals.c

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ arena_t *BLOCK_ARENA;
5959
/* BB_ARENA is responsible for basic_block_t / ph2_ir_t allocation */
6060
arena_t *BB_ARENA;
6161

62+
int global_var_idx = 0;
6263
int bb_label_idx = 0;
6364

6465
ph2_ir_t **PH2_IR_FLATTEN;
@@ -87,6 +88,9 @@ int elf_header_len = 0x54; /* ELF fixed: 0x34 + 1 * 0x20 */
8788
int elf_code_start;
8889
int elf_data_start;
8990

91+
void fatal(char *msg);
92+
void error(char *msg);
93+
9094
/**
9195
* arena_block_create() - Creates a new arena block with given capacity.
9296
* The created arena block is guaranteed to be zero-initialized.
@@ -521,6 +525,85 @@ block_t *add_block(block_t *parent, func_t *func, macro_t *macro)
521525
return blk;
522526
}
523527

528+
char *gen_name_to(char *buf)
529+
{
530+
sprintf(buf, ".t%d", global_var_idx++);
531+
return buf;
532+
}
533+
534+
var_t *init_var(var_t *var)
535+
{
536+
var->consumed = -1;
537+
var->base = var;
538+
return var;
539+
}
540+
541+
var_t *require_var(block_t *blk)
542+
{
543+
var_list_t *var_list = &blk->locals;
544+
545+
if (var_list->size >= var_list->capacity) {
546+
var_list->capacity <<= 1;
547+
548+
var_t **new_locals =
549+
arena_alloc(BLOCK_ARENA, var_list->capacity * sizeof(var_t *));
550+
memcpy(new_locals, var_list->elements,
551+
var_list->size * sizeof(var_t *));
552+
var_list->elements = new_locals;
553+
}
554+
555+
var_t *var = arena_alloc(BLOCK_ARENA, sizeof(var_t));
556+
var_list->elements[var_list->size++] = var;
557+
init_var(var);
558+
var->type = TY_int;
559+
return var;
560+
}
561+
562+
var_t *require_typed_var(block_t *blk, type_t *type)
563+
{
564+
if (!type)
565+
error("Type must not be NULL");
566+
567+
var_t *var = require_var(blk);
568+
var->type = type;
569+
return var;
570+
}
571+
572+
var_t *require_typed_ptr_var(block_t *blk, type_t *type, int ptr)
573+
{
574+
var_t *var = require_typed_var(blk, type);
575+
var->is_ptr = ptr;
576+
return var;
577+
}
578+
579+
var_t *require_ref_var(block_t *blk, type_t *type, int ptr)
580+
{
581+
if (!type)
582+
error("Cannot reference variable from NULL type");
583+
584+
var_t *var = require_typed_var(blk, type);
585+
var->is_ptr = ptr + 1;
586+
return var;
587+
}
588+
589+
var_t *require_deref_var(block_t *blk, type_t *type, int ptr)
590+
{
591+
if (!type)
592+
error("Cannot dereference variable from NULL type");
593+
594+
/* Allowing integer dereferencing */
595+
if (!ptr && type->base_type != TYPE_struct &&
596+
type->base_type != TYPE_typedef)
597+
return require_var(blk);
598+
599+
if (!ptr)
600+
error("Cannot dereference from non-pointer typed variable");
601+
602+
var_t *var = require_typed_var(blk, type);
603+
var->is_ptr = ptr - 1;
604+
return var;
605+
}
606+
524607
void add_alias(char *alias, char *value)
525608
{
526609
alias_t *al = hashmap_get(ALIASES_MAP, alias);

0 commit comments

Comments
 (0)