working on try-catch implementation
This commit is contained in:
		
							
								
								
									
										36
									
								
								lib/comp.c
									
									
									
									
									
								
							
							
						
						
									
										36
									
								
								lib/comp.c
									
									
									
									
									
								
							| @ -1129,7 +1129,6 @@ static int compile_break (hcl_t* hcl, hcl_cnode_t* src) | |||||||
| 		return -1; | 		return -1; | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  |  | ||||||
| 	for (i = hcl->c->cblk.depth; i > hcl->c->fnblk.info[hcl->c->fnblk.depth].cblk_base; --i) | 	for (i = hcl->c->cblk.depth; i > hcl->c->fnblk.info[hcl->c->fnblk.depth].cblk_base; --i) | ||||||
| 	{ | 	{ | ||||||
| 		if (hcl->c->cblk.info[i]._type == HCL_CBLK_TYPE_LOOP) | 		if (hcl->c->cblk.info[i]._type == HCL_CBLK_TYPE_LOOP) | ||||||
| @ -1139,6 +1138,7 @@ static int compile_break (hcl_t* hcl, hcl_cnode_t* src) | |||||||
| 		else if (hcl->c->cblk.info[i]._type == HCL_CBLK_TYPE_TRY) | 		else if (hcl->c->cblk.info[i]._type == HCL_CBLK_TYPE_TRY) | ||||||
| 		{ | 		{ | ||||||
| 			/*must emit an instruction to exit from the try loop.*/ | 			/*must emit an instruction to exit from the try loop.*/ | ||||||
|  |  | ||||||
| 			if (emit_byte_instruction(hcl, HCL_CODE_TRY_EXIT, HCL_CNODE_GET_LOC(src)) <= -1) return -1; | 			if (emit_byte_instruction(hcl, HCL_CODE_TRY_EXIT, HCL_CNODE_GET_LOC(src)) <= -1) return -1; | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| @ -1903,7 +1903,7 @@ static int compile_try (hcl_t* hcl, hcl_cnode_t* src) | |||||||
| 	return 0; | 	return 0; | ||||||
| } | } | ||||||
|  |  | ||||||
| static HCL_INLINE int patch_nearest_post_try (hcl_t* hcl) | static HCL_INLINE int patch_nearest_post_try (hcl_t* hcl, hcl_ooi_t* catch_skip_jip) | ||||||
| { | { | ||||||
| 	hcl_ooi_t jip, block_code_size; | 	hcl_ooi_t jip, block_code_size; | ||||||
| 	hcl_cframe_t* cf; | 	hcl_cframe_t* cf; | ||||||
| @ -1925,9 +1925,11 @@ static HCL_INLINE int patch_nearest_post_try (hcl_t* hcl) | |||||||
| 		if (emit_byte_instruction(hcl, HCL_CODE_PUSH_NIL, HCL_CNODE_GET_LOC(cf->operand)) <= -1) return -1; | 		if (emit_byte_instruction(hcl, HCL_CODE_PUSH_NIL, HCL_CNODE_GET_LOC(cf->operand)) <= -1) return -1; | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	/*jump_inst_pos = hcl-.code.bc.len; */ | 	 | ||||||
| 	if (emit_byte_instruction(hcl, HCL_CODE_TRY_EXIT, HCL_CNODE_GET_LOC(cf->operand)) <= -1) | 	if (emit_byte_instruction(hcl, HCL_CODE_TRY_EXIT, HCL_CNODE_GET_LOC(cf->operand)) <= -1) return -1; | ||||||
| 	if (emit_single_param_instruction(hcl, HCL_CODE_JUMP_FORWARD_0,  MAX_CODE_JUMP, HCL_CNODE_GET_LOC(cmd)) <= -1) return -1; |  | ||||||
|  | 	*catch_skip_jip = hcl->code.bc.len;  | ||||||
|  | 	if (emit_single_param_instruction(hcl, HCL_CODE_JUMP_FORWARD_0,  MAX_CODE_JUMP, HCL_CNODE_GET_LOC(cf->operand)) <= -1) return -1; | ||||||
|  |  | ||||||
| 	/* HCL_CODE_LONG_PARAM_SIZE + 1 => size of the long JUMP_FORWARD instruction */ | 	/* HCL_CODE_LONG_PARAM_SIZE + 1 => size of the long JUMP_FORWARD instruction */ | ||||||
| 	block_code_size = hcl->code.bc.len - jip - (HCL_CODE_LONG_PARAM_SIZE + 1); | 	block_code_size = hcl->code.bc.len - jip - (HCL_CODE_LONG_PARAM_SIZE + 1); | ||||||
| @ -1987,15 +1989,10 @@ static HCL_INLINE int compile_catch (hcl_t* hcl) | |||||||
| 		return -1; | 		return -1; | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	patch_nearest_post_try (hcl); | 	patch_nearest_post_try (hcl, &jump_inst_pos); | ||||||
|  |  | ||||||
| /* TODO: HCL_TRAIT_INTERACTIVE??? */ | /* TODO: HCL_TRAIT_INTERACTIVE??? */ | ||||||
| /* TODO: nargs -> 1 ntmprs  -> 1 */ | /* TODO: nargs -> 1 ntmprs  -> 1 */ | ||||||
| 	if (emit_double_param_instruction(hcl, HCL_CODE_MAKE_BLOCK, 0, 0, HCL_CNODE_GET_LOC(cmd)) <= -1) return -1; |  | ||||||
|  |  | ||||||
| 	jump_inst_pos = hcl->code.bc.len; |  | ||||||
| 	if (emit_single_param_instruction(hcl, HCL_CODE_JUMP_FORWARD_0, MAX_CODE_JUMP, HCL_CNODE_GET_LOC(cmd)) <= -1) return -1; |  | ||||||
|  |  | ||||||
|  |  | ||||||
| 	SWITCH_TOP_CFRAME (hcl, COP_COMPILE_OBJECT_LIST, obj); | 	SWITCH_TOP_CFRAME (hcl, COP_COMPILE_OBJECT_LIST, obj); | ||||||
|  |  | ||||||
| @ -2008,8 +2005,8 @@ static HCL_INLINE int compile_catch (hcl_t* hcl) | |||||||
|  |  | ||||||
| static HCL_INLINE int post_try (hcl_t* hcl) | static HCL_INLINE int post_try (hcl_t* hcl) | ||||||
| { | { | ||||||
|  |  | ||||||
| /* TODO: */ | /* TODO: */ | ||||||
|  | 	pop_cblk (hcl); | ||||||
| 	POP_CFRAME (hcl); | 	POP_CFRAME (hcl); | ||||||
| 	return 0; | 	return 0; | ||||||
| } | } | ||||||
| @ -2024,7 +2021,7 @@ static HCL_INLINE int post_catch (hcl_t* hcl) | |||||||
| 	HCL_ASSERT (hcl, cf->opcode == COP_POST_CATCH); | 	HCL_ASSERT (hcl, cf->opcode == COP_POST_CATCH); | ||||||
| 	HCL_ASSERT (hcl, cf->operand != HCL_NULL); | 	HCL_ASSERT (hcl, cf->operand != HCL_NULL); | ||||||
|  |  | ||||||
| 	jip = cf->u.post_try_catch.jump_inst_pos; | 	jip = cf->u.post_try_catch.jump_inst_pos; /* jump instruction position between the try block and the catch block */ | ||||||
|  |  | ||||||
| 	/* HCL_CODE_LONG_PARAM_SIZE + 1 => size of the long JUMP_FORWARD instruction */ | 	/* HCL_CODE_LONG_PARAM_SIZE + 1 => size of the long JUMP_FORWARD instruction */ | ||||||
| 	block_code_size = hcl->code.bc.len - jip - (HCL_CODE_LONG_PARAM_SIZE + 1); | 	block_code_size = hcl->code.bc.len - jip - (HCL_CODE_LONG_PARAM_SIZE + 1); | ||||||
| @ -2037,24 +2034,13 @@ static HCL_INLINE int post_catch (hcl_t* hcl) | |||||||
| 		block_code_size++; | 		block_code_size++; | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if (emit_byte_instruction(hcl, HCL_CODE_RETURN_FROM_BLOCK, HCL_CNODE_GET_LOC(cf->operand)) <= -1) return -1; |  | ||||||
| 	block_code_size++; |  | ||||||
|  |  | ||||||
| 	if (block_code_size > MAX_CODE_JUMP * 2) | 	if (block_code_size > MAX_CODE_JUMP * 2) | ||||||
| 	{ | 	{ | ||||||
| 		hcl_setsynerrbfmt (hcl, HCL_SYNERR_BLKFLOOD, HCL_CNODE_GET_LOC(cf->operand), HCL_NULL, "code too big - size %zu", block_code_size); | 		hcl_setsynerrbfmt (hcl, HCL_SYNERR_BLKFLOOD, HCL_CNODE_GET_LOC(cf->operand), HCL_NULL, "code too big - size %zu", block_code_size); | ||||||
| 		return -1; | 		return -1; | ||||||
| 	} | 	} | ||||||
| 	patch_long_jump (hcl, jip, block_code_size); |  | ||||||
|  |  | ||||||
| #if 0 | 	patch_long_jump (hcl, jip, block_code_size); /* patch the jump between the try block and the catch block */ | ||||||
| 	if (emit_byte_instruction(hcl, HCL_CODE_TRY_CATCH, HCL_CNODE_GET_LOC(cf->operand)) <= -1) return -1; |  | ||||||
| #endif |  | ||||||
|  |  | ||||||
| #if 0 |  | ||||||
| 	/*hcl->c->fnblk.depth--; */ |  | ||||||
| 	pop_cblk (hcl); |  | ||||||
| #endif |  | ||||||
| 	 | 	 | ||||||
| 	POP_CFRAME (hcl); | 	POP_CFRAME (hcl); | ||||||
| 	return 0; | 	return 0; | ||||||
|  | |||||||
| @ -3121,7 +3121,7 @@ static int execute (hcl_t* hcl) | |||||||
| 			/* -------------------------------------------------------- */ | 			/* -------------------------------------------------------- */ | ||||||
| 			case HCL_CODE_TRY_ENTER: | 			case HCL_CODE_TRY_ENTER: | ||||||
| 				FETCH_PARAM_CODE_TO (hcl, b1); | 				FETCH_PARAM_CODE_TO (hcl, b1); | ||||||
| 				LOG_INST_0 (hcl, "try_enter %zu", b1); | 				LOG_INST_1 (hcl, "try_enter %zu", b1); | ||||||
| #if 0 | #if 0 | ||||||
| 				if (call_try_catch(hcl) <= -1)  | 				if (call_try_catch(hcl) <= -1)  | ||||||
| 				{ | 				{ | ||||||
| @ -3133,7 +3133,7 @@ static int execute (hcl_t* hcl) | |||||||
| 				 | 				 | ||||||
| 			case HCL_CODE_TRY_ENTER2: | 			case HCL_CODE_TRY_ENTER2: | ||||||
| 				FETCH_PARAM_CODE_TO (hcl, b1); | 				FETCH_PARAM_CODE_TO (hcl, b1); | ||||||
| 				LOG_INST_0 (hcl, "try_enter2 %zu", b1); | 				LOG_INST_1 (hcl, "try_enter2 %zu", b1); | ||||||
|  |  | ||||||
| 			case HCL_CODE_TRY_EXIT: | 			case HCL_CODE_TRY_EXIT: | ||||||
| 				LOG_INST_0 (hcl, "try_exit"); | 				LOG_INST_0 (hcl, "try_exit"); | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user