Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 19 additions & 15 deletions src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -2078,10 +2078,19 @@ bool read_global_assignment(char *token)
{
var_t *vd, *rs1, *var;
block_t *parent = GLOBAL_BLOCK;
basic_block_t *bb = GLOBAL_FUNC->bbs;

/* global initialization must be constant */
var = find_global_var(token);
if (var) {
if (lex_peek(T_string, NULL)) {
read_literal_param(parent, bb);
rs1 = opstack_pop();
vd = var;
add_insn(parent, bb, OP_assign, vd, rs1, NULL, 0, NULL);
return true;
}

opcode_t op_stack[10];
opcode_t op, next_op;
int val_stack[10];
Expand All @@ -2094,13 +2103,11 @@ bool read_global_assignment(char *token)
vd = require_var(parent);
gen_name_to(vd->var_name);
vd->init_val = operand1;
add_insn(parent, GLOBAL_FUNC->bbs, OP_load_constant, vd, NULL, NULL,
0, NULL);
add_insn(parent, bb, OP_load_constant, vd, NULL, NULL, 0, NULL);

rs1 = vd;
vd = opstack_pop();
add_insn(parent, GLOBAL_FUNC->bbs, OP_assign, vd, rs1, NULL, 0,
NULL);
add_insn(parent, bb, OP_assign, vd, rs1, NULL, 0, NULL);
return true;
}
if (op == OP_ternary) {
Expand All @@ -2115,13 +2122,11 @@ bool read_global_assignment(char *token)
vd = require_var(parent);
gen_name_to(vd->var_name);
vd->init_val = eval_expression_imm(op, operand1, operand2);
add_insn(parent, GLOBAL_FUNC->bbs, OP_load_constant, vd, NULL, NULL,
0, NULL);
add_insn(parent, bb, OP_load_constant, vd, NULL, NULL, 0, NULL);

rs1 = vd;
vd = opstack_pop();
add_insn(parent, GLOBAL_FUNC->bbs, OP_assign, vd, rs1, NULL, 0,
NULL);
add_insn(parent, bb, OP_assign, vd, rs1, NULL, 0, NULL);
return true;
}
if (op == OP_ternary) {
Expand Down Expand Up @@ -2187,13 +2192,12 @@ bool read_global_assignment(char *token)
vd = require_var(parent);
gen_name_to(vd->var_name);
vd->init_val = val_stack[0];
add_insn(parent, GLOBAL_FUNC->bbs, OP_load_constant, vd,
NULL, NULL, 0, NULL);
add_insn(parent, bb, OP_load_constant, vd, NULL, NULL, 0,
NULL);

rs1 = vd;
vd = opstack_pop();
add_insn(parent, GLOBAL_FUNC->bbs, OP_assign, vd, rs1, NULL,
0, NULL);
add_insn(parent, bb, OP_assign, vd, rs1, NULL, 0, NULL);
}
return true;
}
Expand Down Expand Up @@ -2817,13 +2821,13 @@ void read_global_decl(block_t *block)

/* is a variable */
if (lex_accept(T_assign)) {
if (var->is_ptr == 0 && var->array_size == 0) {
if (var->array_size == 0) {
read_global_assignment(var->var_name);
lex_expect(T_semicolon);
return;
}
/* TODO: support global initialization for array and pointer */
error("Global initialization for array and pointer not supported");
/* TODO: support global initialization for array */
error("Global initialization for array is not supported");
} else if (lex_accept(T_comma))
/* TODO: continuation */
error("Global continuation not supported");
Expand Down
3 changes: 2 additions & 1 deletion src/reg-alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,9 @@ void reg_alloc(void)
}
break;
case OP_load_constant:
case OP_load_data_address:
dest = prepare_dest(GLOBAL_FUNC->bbs, global_insn->rd, -1, -1);
ir = bb_add_ph2_ir(GLOBAL_FUNC->bbs, OP_load_constant);
ir = bb_add_ph2_ir(GLOBAL_FUNC->bbs, global_insn->opcode);
ir->src0 = global_insn->rd->init_val;
ir->dest = dest;
break;
Expand Down
19 changes: 7 additions & 12 deletions src/ssa.c
Original file line number Diff line number Diff line change
Expand Up @@ -880,11 +880,6 @@ void unwind_phi(void)
free(args);
}

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

switch (type) {
case NEXT:
str = &"%s_%p:s->%s_%p:n\n"[0];
str = "%s_%p:s->%s_%p:n\n";
break;
case THEN:
str = &"%s_%p:sw->%s_%p:n\n"[0];
str = "%s_%p:sw->%s_%p:n\n";
break;
case ELSE:
str = &"%s_%p:se->%s_%p:n\n"[0];
str = "%s_%p:se->%s_%p:n\n";
break;
default:
abort();
Expand All @@ -911,20 +906,20 @@ void bb_dump_connection(FILE *fd,
char *pred;
void *pred_id;
if (curr->insn_list.tail) {
pred = &"insn"[0];
pred = "insn";
pred_id = curr->insn_list.tail;
} else {
pred = &"pseudo"[0];
pred = "pseudo";
pred_id = curr;
}

char *succ;
void *succ_id;
if (next->insn_list.tail) {
succ = &"insn"[0];
succ = "insn";
succ_id = next->insn_list.head;
} else {
succ = &"pseudo"[0];
succ = "pseudo";
succ_id = next;
}

Expand Down
13 changes: 13 additions & 0 deletions tests/driver.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1357,6 +1357,19 @@ int main()
}
EOF

# global string initialization and modification
try_output 0 "Hello World!Hallo World!" << EOF
char *data = "Hello World!";

int main(void)
{
printf(data);
data[1] = 'a';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this write operation is invalid because the string literal ("Hello World!") should be read-only and placed in the .rodata section, although the current implementation does not create this section.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to C99 6.4.5 String Literals:

  1. It is unspecified whether these arrays are distinct provided their elements have the appropriate
    values. If the program attempts to modify such an array, the behavior is undefined.

The common practice in other well-known compiler (gcc, clang) suggest that this is indeed an invalid operation since this would cause segmentation fault.

The reason I submitted this initial support patch is to allow my local work to declare string litearl in global scope, which is kind of rough but working anyway. Would you like to look into this behavior patch?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood. I think we can create a patch to add TODO or FIXME comment to inform developers that this write behavior should be fixed and avoided in the future.

printf(data);
return 0;
}
EOF

# global initialization with logical and equality operation
try_ 4 << EOF
int b1 = 1 && 1;
Expand Down