@@ -289,43 +289,22 @@ typedef struct
289289{
290290 cj_ctx * cj ;
291291 function * functions ;
292- int scratch_depth ;
292+ cj_builder_scratch scratch ;
293293} codegen ;
294294
295- static cj_operand acquire_scratch (codegen * cg )
296- {
297- if (cg -> scratch_depth >= 6 )
298- {
299- fprintf (stderr , "scratch register exhausted\n" );
300- exit (1 );
301- }
302- int idx = cg -> scratch_depth ++ ;
303- return cj_builder_scratch_reg ((unsigned )idx );
304- }
305-
306- static void release_scratch (codegen * cg )
307- {
308- if (cg -> scratch_depth <= 0 )
309- {
310- fprintf (stderr , "release underflow\n" );
311- exit (1 );
312- }
313- cg -> scratch_depth -- ;
314- }
315-
316295static cj_operand emit_expr (codegen * cg , node * n )
317296{
318297 switch (n -> kind )
319298 {
320299 case NODE_NUM :
321300 {
322- cj_operand dst = acquire_scratch ( cg );
301+ cj_operand dst = cj_builder_scratch_acquire ( & cg -> scratch );
323302 cj_builder_assign (cg -> cj , dst , cj_make_constant ((uint64_t )(uint32_t )n -> value ));
324303 return dst ;
325304 }
326305 case NODE_PARAM :
327306 {
328- cj_operand dst = acquire_scratch ( cg );
307+ cj_operand dst = cj_builder_scratch_acquire ( & cg -> scratch );
329308 cj_builder_assign (cg -> cj , dst , cj_builder_arg_int (cg -> cj , 0 ));
330309 return dst ;
331310 }
@@ -338,20 +317,20 @@ static cj_operand emit_expr(codegen *cg, node *n)
338317 cj_add (cg -> cj , lhs , rhs );
339318 else
340319 cj_sub (cg -> cj , lhs , rhs );
341- release_scratch ( cg );
320+ cj_builder_scratch_release ( & cg -> scratch );
342321 return lhs ;
343322 }
344323 case NODE_CALL :
345324 {
346325 cj_operand arg = emit_expr (cg , n -> arg );
347326 cj_builder_assign (cg -> cj , cj_builder_arg_int (cg -> cj , 0 ), arg );
348- release_scratch ( cg );
327+ cj_builder_scratch_release ( & cg -> scratch );
349328#if defined(__aarch64__ ) || defined(_M_ARM64 )
350329 cj_bl (cg -> cj , cg -> functions [n -> target ].entry );
351330#else
352331 cj_call (cg -> cj , cg -> functions [n -> target ].entry );
353332#endif
354- cj_operand dst = acquire_scratch ( cg );
333+ cj_operand dst = cj_builder_scratch_acquire ( & cg -> scratch );
355334 cj_builder_assign (cg -> cj , dst , cj_builder_return_reg ());
356335 return dst ;
357336 }
@@ -362,7 +341,7 @@ static cj_operand emit_expr(codegen *cg, node *n)
362341
363342static void emit_function (codegen * cg , function * fn )
364343{
365- cg -> scratch_depth = 0 ;
344+ cj_builder_scratch_init ( & cg -> scratch ) ;
366345 cj_mark_label (cg -> cj , fn -> entry );
367346#if defined(__aarch64__ ) || defined(_M_ARM64 )
368347 cj_operand sp = cj_make_register ("sp" );
@@ -382,7 +361,7 @@ static void emit_function(codegen *cg, function *fn)
382361 cj_operand result = emit_expr (cg , fn -> body );
383362 cj_builder_return_value (cg -> cj , & frame , result );
384363#endif
385- release_scratch ( cg );
364+ cj_builder_scratch_release ( & cg -> scratch );
386365}
387366
388367static const char * program_source = "(def main (x) (sub (call inc x) 3))\n"
@@ -426,7 +405,7 @@ int main(void)
426405 for (int i = 0 ; i < function_count ; i ++ )
427406 functions [i ].entry = cj_create_label (cj );
428407
429- codegen cg = {.cj = cj , .functions = functions , . scratch_depth = 0 };
408+ codegen cg = {.cj = cj , .functions = functions };
430409 emit_function (& cg , & functions [main_idx ]);
431410 for (int i = 0 ; i < function_count ; i ++ )
432411 if (i != main_idx )
0 commit comments