Skip to content

Commit e2ee338

Browse files
jservChAoSUnItY
authored andcommitted
Merge pull request sysprog21#207 from ChAoSUnItY/feat/global_str
Support global string initialization
1 parent 80b1188 commit e2ee338

File tree

4 files changed

+41
-28
lines changed

4 files changed

+41
-28
lines changed

src/parser_c.c

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1989,10 +1989,19 @@ bool read_global_assignment(char *token)
19891989
{
19901990
var_t *vd, *rs1, *var;
19911991
block_t *parent = GLOBAL_BLOCK;
1992+
basic_block_t *bb = GLOBAL_FUNC->bbs;
19921993

19931994
/* global initialization must be constant */
19941995
var = find_global_var(token);
19951996
if (var) {
1997+
if (lex_peek(T_string, NULL)) {
1998+
read_literal_param(parent, bb);
1999+
rs1 = opstack_pop();
2000+
vd = var;
2001+
add_insn(parent, bb, OP_assign, vd, rs1, NULL, 0, NULL);
2002+
return true;
2003+
}
2004+
19962005
opcode_t op_stack[10];
19972006
opcode_t op, next_op;
19982007
int val_stack[10];
@@ -2005,13 +2014,11 @@ bool read_global_assignment(char *token)
20052014
vd = require_var(parent);
20062015
gen_name_to(vd->var_name);
20072016
vd->init_val = operand1;
2008-
add_insn(parent, GLOBAL_FUNC->bbs, OP_load_constant, vd, NULL, NULL,
2009-
0, NULL);
2017+
add_insn(parent, bb, OP_load_constant, vd, NULL, NULL, 0, NULL);
20102018

20112019
rs1 = vd;
20122020
vd = opstack_pop();
2013-
add_insn(parent, GLOBAL_FUNC->bbs, OP_assign, vd, rs1, NULL, 0,
2014-
NULL);
2021+
add_insn(parent, bb, OP_assign, vd, rs1, NULL, 0, NULL);
20152022
return true;
20162023
}
20172024
if (op == OP_ternary) {
@@ -2026,13 +2033,11 @@ bool read_global_assignment(char *token)
20262033
vd = require_var(parent);
20272034
gen_name_to(vd->var_name);
20282035
vd->init_val = eval_expression_imm(op, operand1, operand2);
2029-
add_insn(parent, GLOBAL_FUNC->bbs, OP_load_constant, vd, NULL, NULL,
2030-
0, NULL);
2036+
add_insn(parent, bb, OP_load_constant, vd, NULL, NULL, 0, NULL);
20312037

20322038
rs1 = vd;
20332039
vd = opstack_pop();
2034-
add_insn(parent, GLOBAL_FUNC->bbs, OP_assign, vd, rs1, NULL, 0,
2035-
NULL);
2040+
add_insn(parent, bb, OP_assign, vd, rs1, NULL, 0, NULL);
20362041
return true;
20372042
}
20382043
if (op == OP_ternary) {
@@ -2098,13 +2103,12 @@ bool read_global_assignment(char *token)
20982103
vd = require_var(parent);
20992104
gen_name_to(vd->var_name);
21002105
vd->init_val = val_stack[0];
2101-
add_insn(parent, GLOBAL_FUNC->bbs, OP_load_constant, vd,
2102-
NULL, NULL, 0, NULL);
2106+
add_insn(parent, bb, OP_load_constant, vd, NULL, NULL, 0,
2107+
NULL);
21032108

21042109
rs1 = vd;
21052110
vd = opstack_pop();
2106-
add_insn(parent, GLOBAL_FUNC->bbs, OP_assign, vd, rs1, NULL,
2107-
0, NULL);
2111+
add_insn(parent, bb, OP_assign, vd, rs1, NULL, 0, NULL);
21082112
}
21092113
return true;
21102114
}
@@ -2728,13 +2732,13 @@ void read_global_decl(block_t *block)
27282732

27292733
/* is a variable */
27302734
if (lex_accept(T_assign)) {
2731-
if (var->is_ptr == 0 && var->array_size == 0) {
2735+
if (var->array_size == 0) {
27322736
read_global_assignment(var->var_name);
27332737
lex_expect(T_semicolon);
27342738
return;
27352739
}
2736-
/* TODO: support global initialization for array and pointer */
2737-
error("Global initialization for array and pointer not supported");
2740+
/* TODO: support global initialization for array */
2741+
error("Global initialization for array is not supported");
27382742
} else if (lex_accept(T_comma))
27392743
/* TODO: continuation */
27402744
error("Global continuation not supported");

src/reg-alloc.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,9 @@ void reg_alloc()
283283
}
284284
break;
285285
case OP_load_constant:
286+
case OP_load_data_address:
286287
dest = prepare_dest(GLOBAL_FUNC->bbs, global_insn->rd, -1, -1);
287-
ir = bb_add_ph2_ir(GLOBAL_FUNC->bbs, OP_load_constant);
288+
ir = bb_add_ph2_ir(GLOBAL_FUNC->bbs, global_insn->opcode);
288289
ir->src0 = global_insn->rd->init_val;
289290
ir->dest = dest;
290291
break;

src/ssa.c

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -880,11 +880,6 @@ void unwind_phi()
880880
free(args);
881881
}
882882

883-
/*
884-
* The current cfonrt does not yet support string literal addressing, which
885-
* results in the omission of basic block visualization during the stage-1 and
886-
* stage-2 bootstrapping phases.
887-
*/
888883
#ifdef __SHECC__
889884
#else
890885
void bb_dump_connection(FILE *fd,
@@ -896,13 +891,13 @@ void bb_dump_connection(FILE *fd,
896891

897892
switch (type) {
898893
case NEXT:
899-
str = &"%s_%p:s->%s_%p:n\n"[0];
894+
str = "%s_%p:s->%s_%p:n\n";
900895
break;
901896
case THEN:
902-
str = &"%s_%p:sw->%s_%p:n\n"[0];
897+
str = "%s_%p:sw->%s_%p:n\n";
903898
break;
904899
case ELSE:
905-
str = &"%s_%p:se->%s_%p:n\n"[0];
900+
str = "%s_%p:se->%s_%p:n\n";
906901
break;
907902
default:
908903
abort();
@@ -911,20 +906,20 @@ void bb_dump_connection(FILE *fd,
911906
char *pred;
912907
void *pred_id;
913908
if (curr->insn_list.tail) {
914-
pred = &"insn"[0];
909+
pred = "insn";
915910
pred_id = curr->insn_list.tail;
916911
} else {
917-
pred = &"pseudo"[0];
912+
pred = "pseudo";
918913
pred_id = curr;
919914
}
920915

921916
char *succ;
922917
void *succ_id;
923918
if (next->insn_list.tail) {
924-
succ = &"insn"[0];
919+
succ = "insn";
925920
succ_id = next->insn_list.head;
926921
} else {
927-
succ = &"pseudo"[0];
922+
succ = "pseudo";
928923
succ_id = next;
929924
}
930925

tests/driver.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,6 +1343,19 @@ int main()
13431343
}
13441344
EOF
13451345

1346+
# global string initialization and modification
1347+
try_output 0 "Hello World!Hallo World!" << EOF
1348+
char *data = "Hello World!";
1349+
1350+
int main(void)
1351+
{
1352+
printf(data);
1353+
data[1] = 'a';
1354+
printf(data);
1355+
return 0;
1356+
}
1357+
EOF
1358+
13461359
# global initialization with logical and equality operation
13471360
try_ 4 << EOF
13481361
int b1 = 1 && 1;

0 commit comments

Comments
 (0)