|
| 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 | +} |
0 commit comments