From f6aab3ec1f111cd8d968bdcb7ca800e93b819d24 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 12 Jul 2019 11:38:42 -0300 Subject: First implementation of constant propagation Local constant variables initialized with compile-time constants are optimized away from the code. --- lparser.h | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) (limited to 'lparser.h') diff --git a/lparser.h b/lparser.h index 7d43a813..d9b734bf 100644 --- a/lparser.h +++ b/lparser.h @@ -34,8 +34,9 @@ typedef enum { VNONRELOC, /* expression has its value in a fixed register; info = result register */ VLOCAL, /* local variable; var.ridx = local register; - var.vidx = index in 'actvar.arr' */ + var.vidx = relative index in 'actvar.arr' */ VUPVAL, /* upvalue variable; info = index of upvalue in 'upvalues' */ + VCONST, /* compile-time constant; info = absolute index in 'actvar.arr' */ VINDEXED, /* indexed variable; ind.t = table register; ind.idx = key's R index */ @@ -81,19 +82,25 @@ typedef struct expdesc { } expdesc; +/* kinds of variables */ +#define VDKREG 0 /* regular */ +#define RDKCONST 1 /* constant */ +#define RDKTOCLOSE 2 /* to-be-closed */ +#define RDKCTC 3 /* compile-time constant */ + /* description of an active local variable */ -typedef struct Vardesc { - TValuefields; /* constant value (if it is a compile-time constant) */ - lu_byte ro; /* true if variable is 'const' */ - lu_byte sidx; /* index of the variable in the stack */ - short pidx; /* index of the variable in the Proto's 'locvars' array */ - TString *name; /* variable name */ +typedef union Vardesc { + struct { + TValuefields; /* constant value (if it is a compile-time constant) */ + lu_byte kind; + lu_byte sidx; /* index of the variable in the stack */ + short pidx; /* index of the variable in the Proto's 'locvars' array */ + TString *name; /* variable name */ + } vd; + TValue k; /* constant value (if any) */ } Vardesc; -/* check whether Vardesc is in the stack (not a compile-time constant) */ -#define vdinstack(vd) (ttisnil(vd)) - /* description of pending goto statements and label statements */ typedef struct Labeldesc { -- cgit v1.2.3-55-g6feb