2
0
Эх сурвалжийг харах

Get rid of program counter variable for good

Hugo Musso Gualandi 5 жил өмнө
parent
commit
9c1491bd1f
2 өөрчлөгдсөн 23 нэмэгдсэн , 20 устгасан
  1. 16 9
      src/luaot.c
  2. 7 11
      src/luaot_header.c

+ 16 - 9
src/luaot.c

@@ -566,35 +566,40 @@ void create_function(Proto *p)
     println("  LClosure *cl;");
     println("  TValue *k;");
     println("  StkId base;");
-    println("  const Instruction *pc;");
+    println("  const Instruction *saved_pc;");
     println("  int trap;");
     println("  ");
     println(" tailcall:");
     println("  trap = L->hookmask;");
     println("  cl = clLvalue(s2v(ci->func));");
     println("  k = cl->p->k;");
-    println("  pc = ci->u.l.savedpc;");
+    println("  saved_pc = ci->u.l.savedpc;  /*no explicit program counter*/ " );
     println("  if (trap) {");
     println("    if (cl->p->is_vararg)");
     println("      trap = 0;  /* hooks will start after VARARGPREP instruction */");
-    println("    else if (pc == cl->p->code)  /* first instruction (not resuming)? */");
+    println("    else if (saved_pc == cl->p->code) /*first instruction (not resuming)?*/");
     println("      luaD_hookcall(L, ci);");
     println("    ci->u.l.trap = 1;  /* there may be other hooks */");
     println("  }");
     println("  base = ci->func + 1;");
     println("  /* main loop of interpreter */");
-    println(" ");
     println("  Instruction *function_code = cl->p->code;");
-    println("  Instruction i;  /* instruction being executed */");
-    println("  StkId ra;  /* instruction's A register */");
+    println(" ");
 
-   
     for (int pc = 0; pc < p->sizecode; pc++) {
         Instruction instr = p->code[pc];
         OpCode op = GET_OPCODE(instr);
 
         print_opcode_comment(p, pc);
 
+
+        // While an instruction is executing, the program counter typically
+        // points towards the next instruction. There are some corner cases
+        // where the program counter getss adjusted mid-instruction, but I
+        // am not breaking anything because of those...
+        println("  #undef  LUA_AOT_PC");
+        println("  #define LUA_AOT_PC (function_code + %d)", pc+1);
+
         int next = pc + 1;
         println("  #undef  LUA_AOT_NEXT_JUMP");
         if (next < p->sizecode && GET_OPCODE(p->code[next]) == OP_JMP) {
@@ -608,7 +613,9 @@ void create_function(Proto *p)
         }
 
         println("  label_%02d : {", pc);
-        println("    aot_vmfetch(%d, 0x%08x);", pc, instr);
+        println("    Instruction i = 0x%08x;", instr);
+        println("    StkId ra = RA(i);");
+        println("    (void) ra;");
 
         switch (op) {
             case OP_LOADI: {
@@ -688,7 +695,7 @@ void create_function(Proto *p)
                 println("    updatetrap(ci);");
                 println("    if (trap) {");
                 println("      luaD_hookcall(L, ci);");
-                println("      L->oldpc = pc + 1;  /* next opcode will be seen as a \"new\" line */");
+                println("      L->oldpc = LUA_AOT_PC + 1;  /* next opcode will be seen as a \"new\" line */");
                 println("    }");
                 break;
             }

+ 7 - 11
src/luaot_header.c

@@ -109,24 +109,20 @@
 #define docondjump()	if (cond != GETARG_k(i)) goto LUA_AOT_SKIP1; else donextjump(ci);
 
 //
-// We might want to test what happens if we do not save the pc.
-// This would allow the C compiler to throw away that variable in most cases.
+// The program counter is now known statically at each program point.
 //
 
-// #undef  savepc
-// #define savepc(L)	/* no-op */
+#undef  savepc
+#define savepc(L)	(ci->u.l.savedpc = LUA_AOT_PC)
+
 
 //
 // Our modified "bytecode fetch". Since instr and index are compile time constants,
 // the C compiler should be able to optimize the code in many cases.
 //
 
-#define aot_vmfetch(index, instr) { \
+#define aot_check_trap() { \
   if (trap) {  /* stack reallocation or hooks? */ \
-    trap = luaG_traceexec(L, pc);  /* handle hooks */ \
+    trap = luaG_traceexec(L, LUA_AOT_PC - 1);  /* handle hooks */ \
     updatebase(ci);  /* correct stack */ \
-  } \
-  i = instr; \
-  pc = function_code + index + 1; \
-  ra = RA(i); \
-}
+  }