Skip to content

Commit 8118490

Browse files
committed
Remove deprecated phase 1 IR (ph1_ir_t)
Phase 1 IR is previously served as a solution for bridging C frontend to backend (elf code generation), which becomes obsolete after the introduction of SSA middle-end and the introduction of new phase 1 IR called "insn_t", after this, "ph1_ir_t" is only used for IR dumping purpose. In this patch, "ph1_ir_t" and related structures & functionalities are removed and "insn_t" now covers the original usage of "ph1_ir_t". Additionally, IR dumping is also replace with "insn_t" with SSA-based control flow (by dominator graph traversal), and several opcodes are removed as being only used in "ph1_ir_t" but "insn_t". Notice that shecc still contains 2 phase IR transformation / generation after this patch.
1 parent 4c58d21 commit 8118490

File tree

12 files changed

+526
-1067
lines changed

12 files changed

+526
-1067
lines changed

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ CFLAGS := -O -g \
44
-fwrapv \
55
-Wall -Wextra \
66
-Wno-unused-but-set-variable \
7+
-Wno-unused-parameter \
8+
-Wno-unused-function \
9+
-Wshadow \
710
-Wno-variadic-macros \
811
-Wno-uninitialized \
912
-Wno-strict-prototypes \

README.md

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -176,44 +176,39 @@ Execute the following to generate IR:
176176
$ out/shecc --dump-ir -o fib tests/fib.c
177177
```
178178

179-
Line-by-line explanation between C source and IR:
179+
Line-by-line explanation between C source and IR (variable and label numbering may differ):
180180
```c
181-
C Source IR Explanation
182-
------------------+---------------------------------------+----------------------------------------------------------------------------------
183-
184-
int fib(int n) def int @fib(int %n) Indicate a function definition
185-
{ {
186-
if (n == 0) const %.t1001, $0 Load constant 0 into a temporary variable ".t1001"
187-
%.t1002 = eq %n, %.t1001 Test "n" equals ".t1001" or not, and write the result in temporary variable ".t1002"
188-
br %.t1002, .label.1177, .label.1178 If ".t1002" equals zero, goto false label ".label.1178", otherwise,
189-
goto true label ".label.1177"
190-
.label.1177
191-
return 0; const %.t1003, $0 Load constant 0 into a temporary variable ".t1003"
192-
ret %.t1003 Return ".t1003"
193-
j .label.1184 Jump to endif label ".label.1184"
194-
.label.1178
195-
else if (n == 1) const %.t1004, $1 Load constant 1 into a temporary variable ".t1004"
196-
%.t1005 = eq %n, %.t1004 Test "n" equals ".t1004" or not, and write the result in temporary variable ".t1005"
197-
br %.t1005, .label.1183, .label.1184 If ".t1005" equals zero, goto false label ".label.1184". Otherwise,
198-
goto true label ".label.1183"
199-
.label.1183
200-
return 1; const %.t1006, $1 Load constant 1 into a temporary variable ".t1006"
201-
ret %.t1006 Return ".t1006"
202-
.label.1184
203-
return
204-
fib(n - 1) const %.t1007, $1 Load constant 1 into a temporary variable ".t1007"
205-
%.t1008 = sub %n, %.t1007 Subtract ".t1007" from "n", and store the result in temporary variable ".t1008"
206-
push %.t1008 Prepare parameter for function call
207-
call @fib, 1 Call function "fib" with one parameter
208-
+ retval %.t1009 Store return value in temporary variable ".t1009"
209-
fib(n - 2); const %.t1010, $2 Load constant 2 into a temporary variable ".t1010"
210-
%.t1011 = sub %n, %.t1010 Subtract ".t1010" from "n", and store the result in temporary variable ".t1011"
211-
push %.t1011 Prepare parameter for function call
212-
call @fib, 1 Call function "fib" with one parameter
213-
retval %.t1012 Store return value in temporary variable ".t1012"
214-
%.t1013 = add %.t1009, %.t1012 Add ".t1009" and ".t1012", and store the result in temporary variable ".t1013"
215-
ret %.t1013 Return ".t1013"
216-
} }
181+
C Source IR Explanation
182+
-------------------+--------------------------------------+--------------------------------------------------------------------------------------
183+
int fib(int n) def int @fib(int %n)
184+
{ {
185+
if (n == 0) const %.t871, 0 Load constant 0 into a temporary variable ".t871"
186+
%.t872 = eq %n, %.t871 Test if "n" is equal to ".t871", store result in ".t872"
187+
br %.t872, .label.1430, .label.1431 If ".t872" is non-zero, branch to label ".label.1430", otherwise to ".label.1431"
188+
.label.1430:
189+
return 0; const %.t873, 0 Load constant 0 into a temporary variable ".t873"
190+
ret %.t873 Return ".t873"
191+
.label.1431:
192+
else if (n == 1) const %.t874, 1 Load constant 1 into a temporary variable ".t874"
193+
%.t875 = eq %n, %.t874 Test if "n" is equal to ".t874", store result in ".t875"
194+
br %.t875, .label.1434, .label.1435 If ".t875" is true, branch to ".label.1434", otherwise to ".label.1435"
195+
.label.1434:
196+
return 1; const %.t876, 1 Load constant 1 into a temporary variable ".t876"
197+
ret %.t876 Return ".t876"
198+
.label.1435:
199+
return fib(n - 1) const %.t877, 1 Load constant 1 into ".t877"
200+
%.t878 = sub %n, %.t877 Subtract ".t877" from "n", store in ".t878"
201+
push %.t878 Prepare argument ".t878" for function call
202+
call @fib, 1 Call function "@fib" with 1 argument
203+
+ retval %.t879 Store the return value in ".t879"
204+
fib(n - 2); const %.t880, 2 Load constant 2 into ".t880"
205+
%.t881 = sub %n, %.t880 Subtract ".t880" from "n", store in ".t881"
206+
push %.t881 Prepare argument ".t881" for function call
207+
call @fib, 1 Call function "@fib" with 1 argument
208+
retval %.t882 Store the return value in ".t882"
209+
%.t883 = add %.t879, %.t882 Add ".t879" and ".t882", store in ".t883"
210+
ret %.t883 Return ".t883"
211+
} }
217212
```
218213
219214
## Known Issues

src/defs.h

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -205,12 +205,9 @@ typedef enum {
205205
OP_load_data_address, /* lookup address of a constant in data section */
206206

207207
/* control flow */
208-
OP_label,
209-
OP_branch, /* conditional jump */
210-
OP_jump, /* unconditional jump */
211-
OP_func_ret, /* returned value */
212-
OP_block_start, /* code block start */
213-
OP_block_end, /* code block end */
208+
OP_branch, /* conditional jump */
209+
OP_jump, /* unconditional jump */
210+
OP_func_ret, /* returned value */
214211

215212
/* function pointer */
216213
OP_address_of_func, /* resolve function entry */
@@ -338,17 +335,6 @@ typedef struct {
338335
block_t *tail;
339336
} block_list_t;
340337

341-
/* phase-1 IR definition */
342-
typedef struct {
343-
opcode_t op;
344-
char func_name[MAX_VAR_LEN];
345-
int param_num;
346-
int size;
347-
var_t *dest;
348-
var_t *src0;
349-
var_t *src1;
350-
} ph1_ir_t;
351-
352338
typedef struct basic_block basic_block_t;
353339

354340
/* Definition of a growable buffer for a mutable null-terminated string
@@ -469,8 +455,10 @@ struct basic_block {
469455
insn_list_t insn_list;
470456
ph2_ir_list_t ph2_ir_list;
471457
bb_connection_t prev[MAX_BB_PRED];
472-
struct basic_block *next; /* normal BB */
473-
struct basic_block *then_; /* conditional BB */
458+
char bb_label_name[MAX_VAR_LEN]; /* Used in instruction dumping when ir_dump
459+
is enabled. */
460+
struct basic_block *next; /* normal BB */
461+
struct basic_block *then_; /* conditional BB */
474462
struct basic_block *else_;
475463
struct basic_block *idom;
476464
struct basic_block *r_idom;

0 commit comments

Comments
 (0)