@@ -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,26 @@ 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+ if (label_idx > MAX_LABELS - 1 )
62+ error ("Too many labels in function" );
63+
64+ label_t * l = & labels [label_idx ++ ];
65+ strncpy (l -> label_name , name , MAX_ID_LEN );
66+ l -> bb = bb ;
67+ }
68+
4369char * gen_name_to (char * buf )
4470{
4571 sprintf (buf , ".t%d" , global_var_idx ++ );
@@ -997,6 +1023,60 @@ basic_block_t *handle_while_statement(block_t *parent, basic_block_t *bb)
9971023 return else_ ;
9981024}
9991025
1026+ basic_block_t * handle_goto_statement (block_t * parent , basic_block_t * bb )
1027+ {
1028+ /* Since a goto splits the current program into two basic blocks and makes
1029+ * the subsequent basic block unreachable, this causes problems for later
1030+ * CFG operations. Therefore, we create a fake if that always executes to
1031+ * wrap the goto, and connect the unreachable basic block to the else
1032+ * branch. Finally, return this else block.
1033+ *
1034+ * after:
1035+ * code1;
1036+ * goto label;
1037+ * code2;
1038+ *
1039+ * before:
1040+ * code1;
1041+ * if (1) goto label;
1042+ * code2;
1043+ */
1044+
1045+ char token [MAX_ID_LEN ];
1046+ if (!lex_peek (T_identifier , token ))
1047+ error ("Expected identifier after 'goto'" );
1048+
1049+ lex_expect (T_identifier );
1050+ lex_expect (T_semicolon );
1051+
1052+ basic_block_t * fake_if = bb_create (parent );
1053+ bb_connect (bb , fake_if , NEXT );
1054+ var_t * val = require_var (parent );
1055+ gen_name_to (val -> var_name );
1056+ val -> init_val = 1 ;
1057+ add_insn (parent , fake_if , OP_load_constant , val , NULL , NULL , 0 , NULL );
1058+ add_insn (parent , fake_if , OP_branch , NULL , val , NULL , 0 , NULL );
1059+
1060+ basic_block_t * then_ = bb_create (parent );
1061+ basic_block_t * else_ = bb_create (parent );
1062+ bb_connect (fake_if , then_ , THEN );
1063+ bb_connect (fake_if , else_ , ELSE );
1064+
1065+ add_insn (parent , then_ , OP_jump , NULL , NULL , NULL , 0 , token );
1066+ label_t * label = find_label (token );
1067+ if (label ) {
1068+ label -> used = true;
1069+ bb_connect (then_ , label -> bb , NEXT );
1070+ return else_ ;
1071+ }
1072+
1073+ if (backpatch_bb_idx > MAX_LABELS - 1 )
1074+ error ("Too many forward-referenced labels" );
1075+
1076+ backpatch_bb [backpatch_bb_idx ++ ] = then_ ;
1077+ return else_ ;
1078+ }
1079+
10001080basic_block_t * handle_struct_variable_decl (block_t * parent ,
10011081 basic_block_t * bb ,
10021082 char * token )
@@ -4169,6 +4249,9 @@ basic_block_t *read_body_statement(block_t *parent, basic_block_t *bb)
41694249 return do_while_end ;
41704250 }
41714251
4252+ if (lex_accept (T_goto ))
4253+ return handle_goto_statement (parent , bb );
4254+
41724255 /* empty statement */
41734256 if (lex_accept (T_semicolon ))
41744257 return bb ;
@@ -4753,6 +4836,21 @@ basic_block_t *read_body_statement(block_t *parent, basic_block_t *bb)
47534836 return bb ;
47544837 }
47554838
4839+ if (lex_peek (T_identifier , token )) {
4840+ lex_accept (T_identifier );
4841+ if (lex_accept (T_colon )) {
4842+ label_t * l = find_label (token );
4843+ if (l )
4844+ error ("label redefinition" );
4845+
4846+ basic_block_t * n = bb_create (parent );
4847+ bb_connect (bb , n , NEXT );
4848+ add_label (token , n );
4849+ add_insn (parent , n , OP_label , NULL , NULL , NULL , 0 , token );
4850+ return n ;
4851+ }
4852+ }
4853+
47564854 error ("Unrecognized statement token" );
47574855 return NULL ;
47584856}
@@ -4794,6 +4892,28 @@ void read_func_body(func_t *func)
47944892 basic_block_t * body = read_code_block (func , NULL , NULL , func -> bbs );
47954893 if (body )
47964894 bb_connect (body , func -> exit , NEXT );
4895+
4896+ for (int i = 0 ; i < backpatch_bb_idx ; i ++ ) {
4897+ basic_block_t * bb = backpatch_bb [i ];
4898+ insn_t * g = bb -> insn_list .tail ;
4899+ label_t * label = find_label (g -> str );
4900+ if (!label )
4901+ error ("goto label undefined" );
4902+
4903+ label -> used = true;
4904+ bb_connect (bb , label -> bb , NEXT );
4905+ }
4906+
4907+ for (int i = 0 ; i < label_idx ; i ++ ) {
4908+ label_t * label = & labels [i ];
4909+ if (label -> used )
4910+ continue ;
4911+
4912+ printf ("Warning: unused label %s\n" , label -> label_name );
4913+ }
4914+
4915+ backpatch_bb_idx = 0 ;
4916+ label_idx = 0 ;
47974917}
47984918
47994919/* if first token is type */
0 commit comments