From 4a7e5dc44e9297147ac491c1f747d0fc102585a0 Mon Sep 17 00:00:00 2001
From: hyung-hwan <hyunghwan.chung@gmail.com>
Date: Wed, 27 Jan 2021 16:01:36 +0000
Subject: [PATCH] enhanced the compiler a bit

---
 lib/comp2.c   | 32 ++++++++++++++++++--------------
 lib/hcl-prv.h |  4 ++++
 2 files changed, 22 insertions(+), 14 deletions(-)

diff --git a/lib/comp2.c b/lib/comp2.c
index 60bc343..3c2423a 100644
--- a/lib/comp2.c
+++ b/lib/comp2.c
@@ -2246,13 +2246,14 @@ static int compile_array_list (hcl_t* hcl)
 			cf->u.array_list.index = oldidx + 1;
 		}
 
-		PUSH_SUBCFRAME (hcl, COP_EMIT_POP_INTO_ARRAY, HCL_SMOOI_TO_OOP(oldidx));
+		PUSH_SUBCFRAME (hcl, COP_EMIT_POP_INTO_ARRAY, car);
+		cf = GET_SUBCFRAME(hcl);
+		cf->u.array_list.index = oldidx;
 	}
 
 	return 0;
 }
 
-
 static int compile_bytearray_list (hcl_t* hcl)
 {
 	hcl_cframe2_t* cf;
@@ -2291,7 +2292,9 @@ static int compile_bytearray_list (hcl_t* hcl)
 			cf->u.bytearray_list.index = oldidx + 1;
 		}
 
-		PUSH_SUBCFRAME (hcl, COP_EMIT_POP_INTO_BYTEARRAY, HCL_SMOOI_TO_OOP(oldidx));
+		PUSH_SUBCFRAME (hcl, COP_EMIT_POP_INTO_BYTEARRAY, car);
+		cf = GET_SUBCFRAME(hcl);
+		cf->u.bytearray_list.index = oldidx;
 	}
 
 	return 0;
@@ -2340,7 +2343,7 @@ static int compile_dic_list (hcl_t* hcl)
 			PUSH_SUBCFRAME (hcl, COP_COMPILE_DIC_LIST, cddr);
 		}
 
-		PUSH_SUBCFRAME (hcl, COP_EMIT_POP_INTO_DIC, HCL_SMOOI_TO_OOP(0));
+		PUSH_SUBCFRAME (hcl, COP_EMIT_POP_INTO_DIC, cdr);
 		PUSH_SUBCFRAME(hcl, COP_COMPILE_OBJECT, cadr);
 	}
 
@@ -2363,16 +2366,16 @@ static int compile_qlist (hcl_t* hcl)
 	}
 	else
 	{
-		hcl_cnode_t* car, * cdr;
-
 		if (!HCL_CNODE_IS_CONS(oprnd))
 		{
 			/* the last element after . */
 			SWITCH_TOP_CFRAME (hcl, COP_COMPILE_OBJECT, oprnd);
-			PUSH_SUBCFRAME (hcl, COP_EMIT_POP_INTO_CONS_CDR, oprnd); /* TODO: can i pass the location of the closing )? */
+			PUSH_SUBCFRAME (hcl, COP_EMIT_POP_INTO_CONS_CDR, oprnd);
 		}
 		else
 		{
+			hcl_cnode_t* car, * cdr;
+
 			car = HCL_CNODE_CONS_CAR(oprnd);
 			cdr = HCL_CNODE_CONS_CDR(oprnd);
 
@@ -2380,12 +2383,12 @@ static int compile_qlist (hcl_t* hcl)
 			if (cdr)
 			{
 				PUSH_SUBCFRAME (hcl, COP_COMPILE_QLIST, cdr); /* 3 */
-				PUSH_SUBCFRAME (hcl, COP_EMIT_POP_INTO_CONS, oprnd); /* 2 */
+				PUSH_SUBCFRAME (hcl, COP_EMIT_POP_INTO_CONS, car); /* 2 */
 			}
 			else
 			{
 				/* the last element */
-				PUSH_SUBCFRAME (hcl, COP_EMIT_POP_INTO_CONS_END, oprnd); /* 2 */
+				PUSH_SUBCFRAME (hcl, COP_EMIT_POP_INTO_CONS_END, car); /* 2 */
 			}
 		}
 	}
@@ -2909,9 +2912,9 @@ static HCL_INLINE int emit_pop_into_array (hcl_t* hcl)
 
 	cf = GET_TOP_CFRAME(hcl);
 	HCL_ASSERT (hcl, cf->opcode == COP_EMIT_POP_INTO_ARRAY);
-	HCL_ASSERT (hcl, HCL_OOP_IS_SMOOI(cf->operand));
+	HCL_ASSERT (hcl, cf->operand != HCL_NULL);
 
-	n = emit_single_param_instruction(hcl, HCL_CODE_POP_INTO_ARRAY, HCL_OOP_TO_SMOOI(cf->operand));
+	n = emit_single_param_instruction(hcl, HCL_CODE_POP_INTO_ARRAY, cf->u.array_list.index);
 
 	POP_CFRAME (hcl);
 	return n;
@@ -2924,9 +2927,9 @@ static HCL_INLINE int emit_pop_into_bytearray (hcl_t* hcl)
 
 	cf = GET_TOP_CFRAME(hcl);
 	HCL_ASSERT (hcl, cf->opcode == COP_EMIT_POP_INTO_BYTEARRAY);
-	HCL_ASSERT (hcl, HCL_OOP_IS_SMOOI(cf->operand));
+	HCL_ASSERT (hcl, cf->operand != HCL_NULL);
 
-	n = emit_single_param_instruction(hcl, HCL_CODE_POP_INTO_BYTEARRAY, HCL_OOP_TO_SMOOI(cf->operand));
+	n = emit_single_param_instruction(hcl, HCL_CODE_POP_INTO_BYTEARRAY, cf->u.bytearray_list.index);
 
 	POP_CFRAME (hcl);
 	return n;
@@ -2939,8 +2942,9 @@ static HCL_INLINE int emit_pop_into_dic (hcl_t* hcl)
 
 	cf = GET_TOP_CFRAME(hcl);
 	HCL_ASSERT (hcl, cf->opcode == COP_EMIT_POP_INTO_DIC);
+	HCL_ASSERT (hcl, cf->operand != HCL_NULL);
 
-	n = emit_byte_instruction(hcl, HCL_CODE_POP_INTO_DIC, HCL_NULL);
+	n = emit_byte_instruction(hcl, HCL_CODE_POP_INTO_DIC, HCL_CNODE_GET_LOC(cf->operand));
 
 	POP_CFRAME (hcl);
 	return n;
diff --git a/lib/hcl-prv.h b/lib/hcl-prv.h
index 7b2818e..20e12c7 100644
--- a/lib/hcl-prv.h
+++ b/lib/hcl-prv.h
@@ -337,16 +337,19 @@ struct hcl_cframe2_t
 			hcl_ioloc_t start_loc;
 		} post_if;
 
+		/* COP_COMPILE_ARRAY_LIST, COP_POP_INTO_ARRAY */
 		struct
 		{
 			hcl_ooi_t index;
 		} array_list;
 
+		/* COP_COMPILE_BYTEARRAY_LIST, COP_POP_INTO_BYTEARRAY */
 		struct
 		{
 			hcl_ooi_t index;
 		} bytearray_list;
 
+		/* COP_EMIT_LAMBDA */
 		struct
 		{
 			hcl_oow_t jump_inst_pos;
@@ -354,6 +357,7 @@ struct hcl_cframe2_t
 			hcl_ooi_t lfsize_pos;
 		} lambda;
 
+		/* COP_EMIT_RETURN */
 		struct
 		{
 			int from_home;