Skip to content

Commit 4debd6d

Browse files
authored
Merge pull request #247 from sysprog21/arch-lower
Introduce architecture-specific IR lowering stage
2 parents fe7835b + 2063e73 commit 4debd6d

File tree

5 files changed

+75
-5
lines changed

5 files changed

+75
-5
lines changed

src/arch-lower.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* shecc - Architecture-specific IR lowering stage
3+
*
4+
* Introduces a minimal arch-lowering boundary that applies target-specific
5+
* tweaks to phase-2 IR (ph2_ir) before final code generation. This keeps
6+
* backends simpler by moving decisions that depend on CFG shape or target
7+
* quirks out of emit-time where possible.
8+
*/
9+
10+
#include "../config"
11+
#include "defs.h"
12+
13+
/* ARM-specific lowering:
14+
* - Mark detached conditional branches so codegen can decide between
15+
* short/long forms without re-deriving CFG shape.
16+
*/
17+
void arm_lower(void)
18+
{
19+
for (func_t *func = FUNC_LIST.head; func; func = func->next) {
20+
for (basic_block_t *bb = func->bbs; bb; bb = bb->rpo_next) {
21+
for (ph2_ir_t *insn = bb->ph2_ir_list.head; insn;
22+
insn = insn->next) {
23+
/* Mark branches that don't fall through to next block */
24+
if (insn->op == OP_branch) {
25+
/* In SSA, we index 'else_bb' first, and then 'then_bb' */
26+
insn->is_branch_detached = (insn->else_bb != bb->rpo_next);
27+
}
28+
}
29+
}
30+
}
31+
}
32+
33+
/* RISC-V-specific lowering:
34+
* - Mark detached conditional branches
35+
* - Future: prepare for RISC-V specific patterns
36+
*/
37+
void riscv_lower(void)
38+
{
39+
for (func_t *func = FUNC_LIST.head; func; func = func->next) {
40+
for (basic_block_t *bb = func->bbs; bb; bb = bb->rpo_next) {
41+
for (ph2_ir_t *insn = bb->ph2_ir_list.head; insn;
42+
insn = insn->next) {
43+
/* Mark branches that don't fall through to next block */
44+
if (insn->op == OP_branch)
45+
insn->is_branch_detached = (insn->else_bb != bb->rpo_next);
46+
}
47+
}
48+
}
49+
}
50+
51+
/* Entry point: dispatch to the active architecture. */
52+
void arch_lower(void)
53+
{
54+
#if ELF_MACHINE == 0x28 /* ARM */
55+
arm_lower();
56+
#elif ELF_MACHINE == 0xf3 /* RISC-V */
57+
riscv_lower();
58+
#else
59+
/* Unknown architecture: keep behavior as-is. */
60+
(void) 0;
61+
#endif
62+
}

src/arm-codegen.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,7 @@ void cfg_flatten(void)
172172
flatten_ir->src1 = bb->belong_to->stack_size;
173173
}
174174

175-
if (insn->op == OP_branch) {
176-
/* In SSA, we index 'else_bb' first, and then 'then_bb' */
177-
if (insn->else_bb != bb->rpo_next)
178-
flatten_ir->is_branch_detached = true;
179-
}
175+
/* Branch detachment is determined in the arch-lowering stage */
180176

181177
update_elf_offset(flatten_ir);
182178
}

src/globals.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,9 @@ ph2_ir_t *add_ph2_ir(opcode_t op)
615615
{
616616
ph2_ir_t *ph2_ir = arena_alloc(BB_ARENA, sizeof(ph2_ir_t));
617617
ph2_ir->op = op;
618+
/* Set safe defaults; arch-lowering may annotate later */
619+
ph2_ir->next = NULL;
620+
ph2_ir->is_branch_detached = 0;
618621
return add_existed_ph2_ir(ph2_ir);
619622
}
620623

src/main.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
/* Peephole optimization */
3838
#include "peephole.c"
3939

40+
/* Arch-specific IR lowering boundary */
41+
#include "arch-lower.c"
42+
4043
/* Machine code generation. support ARMv7-A and RV32I */
4144
#include "codegen.c"
4245

@@ -103,6 +106,9 @@ int main(int argc, char *argv[])
103106

104107
peephole();
105108

109+
/* Apply arch-specific IR tweaks before final codegen */
110+
arch_lower();
111+
106112
/* flatten CFG to linear instruction */
107113
cfg_flatten();
108114

src/reg-alloc.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ ph2_ir_t *bb_add_ph2_ir(basic_block_t *bb, opcode_t op)
5555
{
5656
ph2_ir_t *n = arena_alloc(BB_ARENA, sizeof(ph2_ir_t));
5757
n->op = op;
58+
/* Ensure deterministic defaults for newly created IR nodes */
59+
n->next = NULL; /* well-formed singly linked list */
60+
n->is_branch_detached = 0; /* arch-lowering will set for branches */
5861

5962
if (!bb->ph2_ir_list.head)
6063
bb->ph2_ir_list.head = n;

0 commit comments

Comments
 (0)