@@ -26,6 +26,12 @@ int break_exit_idx = 0;
2626basic_block_t * continue_bb [MAX_NESTING ];
2727int continue_pos_idx = 0 ;
2828
29+ /* Label utilities */
30+ label_t labels [MAX_LABELS ];
31+ int label_idx = 0 ;
32+ basic_block_t * backpatch_bb [MAX_LABELS ];
33+ int backpatch_bb_idx = 0 ;
34+
2935/* stack of the operands of 3AC */
3036var_t * operand_stack [MAX_OPERAND_STACK_SIZE ];
3137int operand_stack_idx = 0 ;
@@ -40,6 +46,23 @@ void parse_array_init(var_t *var,
4046 basic_block_t * * bb ,
4147 bool emit_code );
4248
49+
50+ label_t * find_label (char * name )
51+ {
52+ for (int i = 0 ; i < label_idx ; i ++ ) {
53+ if (!strcmp (name , labels [i ].label_name ))
54+ return & labels [i ];
55+ }
56+ return NULL ;
57+ }
58+
59+ void add_label (char * name , basic_block_t * bb )
60+ {
61+ label_t * l = & labels [label_idx ++ ];
62+ strncpy (l -> label_name , name , MAX_ID_LEN );
63+ l -> bb = bb ;
64+ }
65+
4366char * gen_name_to (char * buf )
4467{
4568 sprintf (buf , ".t%d" , global_var_idx ++ );
@@ -997,6 +1020,58 @@ basic_block_t *handle_while_statement(block_t *parent, basic_block_t *bb)
9971020 return else_ ;
9981021}
9991022
1023+ basic_block_t * handle_goto_statement (block_t * parent , basic_block_t * bb )
1024+ {
1025+ /* Since a goto splits the current program into two basic blocks and makes
1026+ * the subsequent basic block unreachable, this causes problems for later
1027+ * CFG operations. Therefore, we create a fake if that always executes to
1028+ * wrap the goto, and connect the unreachable basic block to the else
1029+ * branch. Finally, return this else block.
1030+ *
1031+ * after:
1032+ * code1;
1033+ * goto label;
1034+ * code2;
1035+ *
1036+ * before:
1037+ * code1;
1038+ * if (1) goto label;
1039+ * code2;
1040+ */
1041+
1042+ char token [MAX_ID_LEN ];
1043+ if (lex_peek (T_identifier , token )) {
1044+ lex_expect (T_identifier );
1045+ lex_expect (T_semicolon );
1046+
1047+ basic_block_t * fake_if = bb_create (parent );
1048+ bb_connect (bb , fake_if , NEXT );
1049+ var_t * val = require_var (parent );
1050+ gen_name_to (val -> var_name );
1051+ val -> init_val = 1 ;
1052+ add_insn (parent , fake_if , OP_load_constant , val , NULL , NULL , 0 , NULL );
1053+ add_insn (parent , fake_if , OP_branch , NULL , val , NULL , 0 , NULL );
1054+
1055+ basic_block_t * then_ = bb_create (parent );
1056+ basic_block_t * else_ = bb_create (parent );
1057+ bb_connect (fake_if , then_ , THEN );
1058+ bb_connect (fake_if , else_ , ELSE );
1059+
1060+ label_t * label = find_label (token );
1061+ add_insn (parent , then_ , OP_jump , NULL , NULL , NULL , 0 , token );
1062+ if (label ) {
1063+ label -> used = true;
1064+ bb_connect (then_ , label -> bb , NEXT );
1065+ } else {
1066+ backpatch_bb [backpatch_bb_idx ++ ] = then_ ;
1067+ }
1068+ return else_ ;
1069+ }
1070+
1071+ error ("Expected identifier after 'goto'" );
1072+ return NULL ;
1073+ }
1074+
10001075basic_block_t * handle_struct_variable_decl (block_t * parent ,
10011076 basic_block_t * bb ,
10021077 char * token )
@@ -4169,6 +4244,9 @@ basic_block_t *read_body_statement(block_t *parent, basic_block_t *bb)
41694244 return do_while_end ;
41704245 }
41714246
4247+ if (lex_accept (T_goto ))
4248+ return handle_goto_statement (parent , bb );
4249+
41724250 /* empty statement */
41734251 if (lex_accept (T_semicolon ))
41744252 return bb ;
@@ -4753,6 +4831,22 @@ basic_block_t *read_body_statement(block_t *parent, basic_block_t *bb)
47534831 return bb ;
47544832 }
47554833
4834+ if (lex_peek (T_identifier , token )) {
4835+ lex_accept (T_identifier );
4836+ if (lex_accept (T_colon )) {
4837+ label_t * l = find_label (token );
4838+ if (l ) {
4839+ error ("label redefinition" );
4840+ return NULL ;
4841+ }
4842+ basic_block_t * n = bb_create (parent );
4843+ bb_connect (bb , n , NEXT );
4844+ add_label (token , n );
4845+ add_insn (parent , n , OP_label , NULL , NULL , NULL , 0 , token );
4846+ return n ;
4847+ }
4848+ }
4849+
47564850 error ("Unrecognized statement token" );
47574851 return NULL ;
47584852}
@@ -4794,6 +4888,27 @@ void read_func_body(func_t *func)
47944888 basic_block_t * body = read_code_block (func , NULL , NULL , func -> bbs );
47954889 if (body )
47964890 bb_connect (body , func -> exit , NEXT );
4891+
4892+ for (int i = 0 ; i < backpatch_bb_idx ; i ++ ) {
4893+ basic_block_t * bb = backpatch_bb [i ];
4894+ insn_t * g = bb -> insn_list .tail ;
4895+ label_t * label = find_label (g -> str );
4896+ if (!label ) {
4897+ error ("goto label undefined" );
4898+ } else {
4899+ label -> used = true;
4900+ bb_connect (bb , label -> bb , NEXT );
4901+ }
4902+ }
4903+
4904+ for (int i = 0 ; i < label_idx ; i ++ ) {
4905+ label_t * label = & labels [i ];
4906+ if (!label -> used )
4907+ printf ("Warning: unused label %s\n" , label -> label_name );
4908+ }
4909+
4910+ backpatch_bb_idx = 0 ;
4911+ label_idx = 0 ;
47974912}
47984913
47994914/* if first token is type */
0 commit comments