op_forprep work

Dibyendu Majumdar 9 years ago
parent 356ead5990
commit 711a0b63eb

@ -0,0 +1,508 @@
extern int printf(const char *, ...);
struct GCObject {
struct GCObject *next;
unsigned char tt;
unsigned char marked;
};
struct lua_State;
union Value {
struct GCObject *gc;
void *p;
int b;
int (*f)(struct lua_State *);
long long i;
double n;
};
struct TValue {
union Value value_;
int tt_;
};
struct TString {
struct GCObject *next;
unsigned char tt;
unsigned char marked;
unsigned char extra;
unsigned int hash;
unsigned long long len;
struct TString *hnext;
};
union MaxAlign {
double u;
void *s;
long long i;
long l;
};
union UTString {
union MaxAlign dummy;
struct TString tsv;
};
struct Udata {
struct GCObject *next;
unsigned char tt;
unsigned char marked;
unsigned char ttuv_; /* user value's tag */
struct Table *metatable;
unsigned long long len; /* number of bytes */
union Value user_; /* user value */
};
union UUdata {
union MaxAlign dummy; /* ensures maximum alignment for 'local' udata */
struct Udata uv;
};
struct Upvaldesc {
struct TString *name; /* upvalue name (for debug information) */
unsigned char instack; /* whether it is in stack */
unsigned char
idx; /* index of upvalue (in stack or in outer function's list) */
};
enum ravitype_t {
RAVI_TANY, /* Lua dynamic type */
RAVI_TNUMINT, /* integer number */
RAVI_TNUMFLT, /* floating point number */
RAVI_TARRAYINT, /* array of ints */
RAVI_TARRAYFLT, /* array of doubles */
RAVI_TFUNCTION,
RAVI_TTABLE,
RAVI_TSTRING,
RAVI_TNIL,
RAVI_TBOOLEAN
};
struct LocVar {
struct TString *varname;
int startpc; /* first point where variable is active */
int endpc; /* first point where variable is dead */
enum ravitype_t
ravi_type; /* RAVI type of the variable - RAVI_TANY if unknown */
};
struct Proto {
struct GCObject *next;
unsigned char tt;
unsigned char marked;
unsigned char numparams; /* number of fixed parameters */
unsigned char is_vararg;
unsigned char maxstacksize; /* maximum stack used by this function */
int sizeupvalues; /* size of 'upvalues' */
int sizek; /* size of 'k' */
int sizecode;
int sizelineinfo;
int sizep; /* size of 'p' */
int sizelocvars;
int linedefined;
int lastlinedefined;
struct TValue *k; /* constants used by the function */
unsigned int *code;
struct Proto **p; /* functions defined inside the function */
int *lineinfo; /* map from opcodes to source lines (debug information) */
struct LocVar *
locvars; /* information about local variables (debug information) */
struct Upvaldesc *upvalues; /* upvalue information */
struct LClosure *cache; /* last created closure with this prototype */
struct TString *source; /* used for debug information */
struct GCObject *gclist;
} Proto;
struct UpVal;
struct CClosure {
struct GCObject *next;
unsigned char tt;
unsigned char marked;
unsigned char nupvalues;
struct GCObject *gclist;
int (*f)(struct lua_State *);
struct TValue upvalue[1]; /* list of upvalues */
};
struct LClosure {
struct GCObject *next;
unsigned char tt;
unsigned char marked;
unsigned char nupvalues;
struct GCObject *gclist;
struct Proto *p;
struct UpVal *upvals[1]; /* list of upvalues */
};
union Closure {
struct CClosure c;
struct LClosure l;
};
union TKey {
struct {
union Value value_;
int tt_;
int next; /* for chaining (offset for next node) */
} nk;
struct TValue tvk;
};
struct Node {
struct TValue i_val;
union TKey i_key;
};
struct Table {
struct GCObject *next;
unsigned char tt;
unsigned char marked;
unsigned char flags; /* 1<<p means tagmethod(p) is not present */
unsigned char lsizenode; /* log2 of size of 'node' array */
unsigned int sizearray; /* size of 'array' array */
struct TValue *array; /* array part */
struct Node *node;
struct Node *lastfree; /* any free position is before this position */
struct Table *metatable;
struct GCObject *gclist;
enum ravitype_t ravi_array_type; /* RAVI specialization */
unsigned int ravi_array_len; /* RAVI len specialization */
};
struct Mbuffer {
char *buffer;
unsigned long long n;
unsigned long long buffsize;
};
struct stringtable {
struct TString **hash;
int nuse; /* number of elements */
int size;
};
struct lua_Debug;
typedef long long ptrdiff_t;
typedef ptrdiff_t lua_KContext;
typedef int (*lua_KFunction)(struct lua_State *L, int status, lua_KContext ctx);
typedef void *(*lua_Alloc)(void *ud, void *ptr, unsigned long long osize,
unsigned long long nsize);
typedef void (*lua_Hook)(struct lua_State *L, struct lua_Debug *ar);
struct CallInfoL { /* only for Lua functions */
struct TValue *base; /* base for this function */
const unsigned int *savedpc;
ptrdiff_t dummy;
};
struct CallInfoC { /* only for C functions */
lua_KFunction k; /* continuation in case of yields */
ptrdiff_t old_errfunc;
lua_KContext ctx; /* context info. in case of yields */
};
struct CallInfo {
struct TValue *func; /* function index in the stack */
struct TValue *top; /* top for this function */
struct CallInfo *previous, *next; /* dynamic call link */
union {
struct CallInfoL l;
struct CallInfoC c;
} u;
ptrdiff_t extra;
short nresults; /* expected number of results from this function */
unsigned char callstatus;
};
struct CallInfoLua {
struct TValue *func; /* function index in the stack */
struct TValue *top; /* top for this function */
struct CallInfo *previous, *next; /* dynamic call link */
struct CallInfoL l;
ptrdiff_t extra;
short nresults; /* expected number of results from this function */
unsigned char callstatus;
};
struct global_State;
struct lua_longjmp;
/*
** 'per thread' state
*/
struct lua_State {
struct GCObject *next;
unsigned char tt;
unsigned char marked;
unsigned char status;
struct TValue *top; /* first free slot in the stack */
struct global_State *l_G;
struct CallInfoLua *ci; /* call info for current function */
const unsigned int *oldpc; /* last pc traced */
struct TValue *stack_last; /* last free slot in the stack */
struct TValue *stack; /* stack base */
struct UpVal *openupval; /* list of open upvalues in this stack */
struct GCObject *gclist;
struct lua_State *twups; /* list of threads with open upvalues */
struct lua_longjmp *errorJmp; /* current error recover point */
struct CallInfo base_ci; /* CallInfo for first level (C calling Lua) */
lua_Hook hook;
ptrdiff_t errfunc; /* current error handling function (stack index) */
int stacksize;
int basehookcount;
int hookcount;
unsigned short nny; /* number of non-yieldable calls in stack */
unsigned short nCcalls; /* number of nested C calls */
unsigned char hookmask;
unsigned char allowhook;
};
void testfunc(struct GCObject *obj) { printf("value = %d\n", obj->tt); }
extern int luaD_poscall(struct lua_State *L, struct TValue *ra);
/*
The following represents a C version of the Lua function:
local function x()
local j = 0
for i=1,5 do
j = i
end
return j
end
The Lua byte codes are:
function <(string):1,1> (9 instructions at 00000014E5C8E3
0 params, 5 slots, 0 upvalues, 5 locals, 3 constants, 0 f
1 [1] LOADK 0 -1 ; 0
2 [1] LOADK 1 -2 ; 1
3 [1] LOADK 2 -3 ; 5
4 [1] LOADK 3 -2 ; 1
5 [1] FORPREP 1 1 ; to 7
6 [1] MOVE 0 4
7 [1] FORLOOP 1 -2 ; to 6
8 [1] RETURN 0 2
9 [1] RETURN 0 1
constants (3) for 00000014E5C8E380:
1 0
2 1
3 5
locals (5) for 00000014E5C8E380:
0 j 2 10
1 (for index) 5 8
2 (for limit) 5 8
3 (for step) 5 8
4 i 6 7
upvalues (0) for 00000014E5C8E380:
*/
extern int forlimit(const struct TValue *obj, long long int *p, long long int step,
int *stopnow);
extern int luaV_tonumber_(const struct TValue *obj, double *n);
void test1(struct lua_State *L) {
/* This is the function prologue */
struct CallInfoLua *ci;
struct LClosure *cl;
struct TValue *k;
struct TValue *base;
struct CallInfoL *cil;
//struct TValue *ra, *rb, *rc;
int b;
ci = L->ci;
base = ci->l.base;
cl = (struct LClosure *)(ci->func->value_.gc);
k = cl->p->k;
// // 1[1] LOADK 0 -1; 0
// ra = base + 0;
// rb = k + 0;
// *ra = *rb;
// // 2[1] LOADK 1 -2; 1
// ra = base + 1;
// rb = k + 1;
// *ra = *rb;
// // 3[1] LOADK 2 -3; 5
// ra = base + 2;
// rb = k + 2;
// *ra = *rb;
// // 4[1] LOADK 3 -2; 1
// ra = base + 3;
// rb = k + 1;
// *ra = *rb;
// 5[1] FORPREP 1 1; to 7
//TValue *init = ra;
//TValue *plimit = ra + 1;
//TValue *pstep = ra + 2;
//lua_Integer ilimit;
//int stopnow;
//if (ttisinteger(init) && ttisinteger(pstep) &&
// forlimit(plimit, &ilimit, ivalue(pstep), &stopnow)) {
// /* all values are integer */
// lua_Integer initv = (stopnow ? 0 : ivalue(init));
// setivalue(plimit, ilimit);
// setivalue(init, initv - ivalue(pstep));
//}
//else { /* try making all values floats */
// lua_Number ninit; lua_Number nlimit; lua_Number nstep;
// if (!tonumber(plimit, &nlimit))
// luaG_runerror(L, "'for' limit must be a number");
// setfltvalue(plimit, nlimit);
// if (!tonumber(pstep, &nstep))
// luaG_runerror(L, "'for' step must be a number");
// setfltvalue(pstep, nstep);
// if (!tonumber(init, &ninit))
// luaG_runerror(L, "'for' initial value must be a number");
// setfltvalue(init, luai_numsub(L, ninit, nstep));
//}
//ci->u.l.savedpc += GETARG_sBx(i);
int A, A1, A2;
label_forprep:
A = 1;
A1 = A+1;
A2 = A+2;
struct TValue *init = base + A;
struct TValue *plimit = base + A1;
struct TValue *pstep = base + A2;
long long int ilimit;
int stopnow;
#define LUA_NUMINT (3 | 1 << 4)
#define LUA_NUMFLT (3)
int init_is_integer = init->tt_ == LUA_NUMINT;
int pstep_is_integer = pstep->tt_ == LUA_NUMINT;
int fl = forlimit(plimit, &ilimit, pstep->value_.i, &stopnow);
if (init_is_integer && pstep_is_integer && fl) {
long long int initv = (stopnow ? 0 : init->value_.i);
plimit->value_.i = ilimit; plimit->tt_ = LUA_NUMINT;
init->value_.i = initv - pstep->value_.i; init->tt_ = LUA_NUMINT;
}
else {
double ninit;
double nlimit;
double nstep;
int plimit_is_float = 0;
if (plimit->tt_ == LUA_NUMFLT) {
plimit_is_float = 1;
nlimit = plimit->value_.n;
}
else {
plimit_is_float = luaV_tonumber_(plimit, &nlimit);
}
if (!plimit_is_float)
luaG_runerror(L, "'for' limit must be a number");
plimit->value_.n = nlimit;
plimit->tt_ = LUA_NUMFLT;
int pstep_is_float = 0;
if (pstep->tt_ == LUA_NUMFLT) {
pstep_is_float = 1;
nstep = pstep->value_.n;
}
else {
pstep_is_float = luaV_tonumber_(pstep, &nstep);
}
if (!pstep_is_float)
luaG_runerror(L, "'for' step must be a number");
pstep->value_.n = nstep;
pstep->tt_ = LUA_NUMFLT;
int init_is_float = 0;
if (init->tt_ == LUA_NUMFLT) {
init_is_float = 1;
ninit = init->value_.n;
}
else {
init_is_float = luaV_tonumber_(init, &ninit);
}
if (!init_is_float)
luaG_runerror(L, "'for' initial value must be a number");
init->value_.n = ninit - nstep;
init->tt_ = LUA_NUMFLT;
}
goto label_forloop;
// label_loopbody:
// // 6[1] MOVE 0 4
// ra = base + 0;
// rb = base + 4;
// *ra = *rb;
label_forloop:
// 7[1] FORLOOP 1 - 2; to 6
//if (ttisinteger(ra)) { /* integer loop? */
// lua_Integer step = ivalue(ra + 2);
// lua_Integer idx = ivalue(ra) + step; /* increment index */
// lua_Integer limit = ivalue(ra + 1);
// if ((0 < step) ? (idx <= limit) : (limit <= idx)) {
// ci->u.l.savedpc += GETARG_sBx(i); /* jump back */
// setivalue(ra, idx); /* update internal index... */
// setivalue(ra + 3, idx); /* ...and external index */
// }
//}
//else { /* floating loop */
// lua_Number step = fltvalue(ra + 2);
// lua_Number idx = luai_numadd(L, fltvalue(ra), step); /* inc. index */
// lua_Number limit = fltvalue(ra + 1);
// if (luai_numlt(0, step) ? luai_numle(idx, limit)
// : luai_numle(limit, idx)) {
// ci->u.l.savedpc += GETARG_sBx(i); /* jump back */
// setfltvalue(ra, idx); /* update internal index... */
// setfltvalue(ra + 3, idx); /* ...and external index */
// }
//}
// ra = base + 1;
// if (ra->tt_ == LUA_NUMINT) {
// rb = ra + 2;
// long long int step = rb->value_.i;
// long long int idx = ra->value_.i + step;
// rc = ra + 1;
// long long int limit = rc->value_.i;
// if ((0 < step) ? (idx <= limit) : (limit <= idx)) {
// ra->value_.i = idx;
// ra->tt_ = LUA_NUMINT;
// rc = ra + 3;
// rc->value_.i = idx;
// rc->tt_ = LUA_NUMINT;
// goto label_loopbody;
// }
// }
// else {
// rb = ra + 2;
// double step = rb->value_.n;
// double idx = ra->value_.n + step;
// rc = ra + 1;
// double limit = rc->value_.n;
// if ((0 < step) ? (idx <= limit) : (limit <= idx)) {
// ra->value_.n = idx;
// ra->tt_ = LUA_NUMFLT;
// rc = ra + 3;
// rc->value_.n = idx;
// rc->tt_ = LUA_NUMFLT;
// goto label_loopbody;
// }
// }
// // 8[1] RETURN 0 2
// b = 2;
// ra = base + 0;
// // if (b)
// L->top = ra + b - 1;
// if (cl->p->sizep > 0)
// luaF_close(L, base);
// b = luaD_poscall(L, ra);
// if (b)
// L->top = ci->top;
return;
}

@ -0,0 +1,209 @@
; ModuleID = 'lua_op_forprep.c'
target datalayout = "e-m:w-p:32:32-i64:64-f80:32-n8:16:32-S32"
target triple = "i686-pc-windows-gnu"
%struct.Proto = type { %struct.GCObject*, i8, i8, i8, i8, i8, i32, i32, i32, i32, i32, i32, i32, i32, %struct.TValue*, i32*, %struct.Proto**, i32*, %struct.LocVar*, %struct.Upvaldesc*, %struct.LClosure*, %struct.TString*, %struct.GCObject* }
%struct.TValue = type { %union.Value, i32 }
%union.Value = type { i64 }
%struct.LocVar = type { %struct.TString*, i32, i32, i32 }
%struct.Upvaldesc = type { %struct.TString*, i8, i8 }
%struct.LClosure = type { %struct.GCObject*, i8, i8, i8, %struct.GCObject*, %struct.Proto*, [1 x %struct.UpVal*] }
%struct.UpVal = type opaque
%struct.TString = type { %struct.GCObject*, i8, i8, i8, i32, i64, %struct.TString* }
%struct.GCObject = type { %struct.GCObject*, i8, i8 }
%struct.lua_State = type { %struct.GCObject*, i8, i8, i8, %struct.TValue*, %struct.global_State*, %struct.CallInfoLua*, i32*, %struct.TValue*, %struct.TValue*, %struct.UpVal*, %struct.GCObject*, %struct.lua_State*, %struct.lua_longjmp*, %struct.CallInfo, void (%struct.lua_State*, %struct.lua_Debug*)*, i64, i32, i32, i32, i16, i16, i8, i8 }
%struct.global_State = type opaque
%struct.CallInfoLua = type { %struct.TValue*, %struct.TValue*, %struct.CallInfo*, %struct.CallInfo*, %struct.CallInfoL, i64, i16, i8 }
%struct.CallInfoL = type { %struct.TValue*, i32*, i64 }
%struct.lua_longjmp = type opaque
%struct.CallInfo = type { %struct.TValue*, %struct.TValue*, %struct.CallInfo*, %struct.CallInfo*, %union.anon, i64, i16, i8 }
%union.anon = type { %struct.CallInfoC }
%struct.CallInfoC = type { i32 (%struct.lua_State*, i32, i64)*, i64, i64 }
%struct.lua_Debug = type opaque
@.str = private unnamed_addr constant [12 x i8] c"value = %d\0A\00", align 1
@.str1 = private unnamed_addr constant [29 x i8] c"'for' limit must be a number\00", align 1
@.str2 = private unnamed_addr constant [28 x i8] c"'for' step must be a number\00", align 1
@.str3 = private unnamed_addr constant [37 x i8] c"'for' initial value must be a number\00", align 1
@Proto = common global %struct.Proto zeroinitializer, align 4
; Function Attrs: nounwind
define void @testfunc(%struct.GCObject* nocapture readonly %obj) #0 {
entry:
%tt = getelementptr inbounds %struct.GCObject* %obj, i32 0, i32 1
%0 = load i8* %tt, align 1, !tbaa !1
%conv = zext i8 %0 to i32
%call = tail call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([12 x i8]* @.str, i32 0, i32 0), i32 %conv) #2
ret void
}
; Function Attrs: nounwind
declare i32 @printf(i8* nocapture readonly, ...) #0
; Function Attrs: nounwind
define void @test1(%struct.lua_State* %L) #0 {
entry:
%ilimit = alloca i64, align 8
%stopnow = alloca i32, align 4
%ninit = alloca double, align 8
%nlimit = alloca double, align 8
%nstep = alloca double, align 8
%ci1 = getelementptr inbounds %struct.lua_State* %L, i32 0, i32 6
%0 = load %struct.CallInfoLua** %ci1, align 4, !tbaa !6
%base2 = getelementptr inbounds %struct.CallInfoLua* %0, i32 0, i32 4, i32 0
%1 = load %struct.TValue** %base2, align 4, !tbaa !12
%add.ptr = getelementptr inbounds %struct.TValue* %1, i32 1
%add.ptr5 = getelementptr inbounds %struct.TValue* %1, i32 2
%add.ptr6 = getelementptr inbounds %struct.TValue* %1, i32 3
%tt_ = getelementptr inbounds %struct.TValue* %1, i32 1, i32 1
%2 = load i32* %tt_, align 4, !tbaa !15
%cmp = icmp eq i32 %2, 19
%tt_7 = getelementptr inbounds %struct.TValue* %1, i32 3, i32 1
%3 = load i32* %tt_7, align 4, !tbaa !15
%cmp8 = icmp eq i32 %3, 19
%i = getelementptr inbounds %struct.TValue* %add.ptr6, i32 0, i32 0, i32 0
%4 = load i64* %i, align 8, !tbaa !17
%call = call i32 @forlimit(%struct.TValue* %add.ptr5, i64* %ilimit, i64 %4, i32* %stopnow) #2
%or.cond = and i1 %cmp, %cmp8
%tobool13 = icmp ne i32 %call, 0
%or.cond73 = and i1 %or.cond, %tobool13
br i1 %or.cond73, label %if.then, label %if.else
if.then: ; preds = %entry
%5 = load i32* %stopnow, align 4, !tbaa !18
%tobool14 = icmp eq i32 %5, 0
%i16 = getelementptr inbounds %struct.TValue* %add.ptr, i32 0, i32 0, i32 0
br i1 %tobool14, label %cond.false, label %cond.end
cond.false: ; preds = %if.then
%6 = load i64* %i16, align 8, !tbaa !17
br label %cond.end
cond.end: ; preds = %if.then, %cond.false
%cond = phi i64 [ %6, %cond.false ], [ 0, %if.then ]
%7 = load i64* %ilimit, align 8, !tbaa !17
%i18 = getelementptr inbounds %struct.TValue* %add.ptr5, i32 0, i32 0, i32 0
store i64 %7, i64* %i18, align 8, !tbaa !17
%tt_19 = getelementptr inbounds %struct.TValue* %1, i32 2, i32 1
store i32 19, i32* %tt_19, align 4, !tbaa !15
%8 = load i64* %i, align 8, !tbaa !17
%sub = sub nsw i64 %cond, %8
store i64 %sub, i64* %i16, align 8, !tbaa !17
br label %label_forloop
if.else: ; preds = %entry
%tt_25 = getelementptr inbounds %struct.TValue* %1, i32 2, i32 1
%9 = load i32* %tt_25, align 4, !tbaa !15
%cmp26 = icmp eq i32 %9, 3
br i1 %cmp26, label %if.then28, label %if.else30
if.then28: ; preds = %if.else
%n = bitcast %struct.TValue* %add.ptr5 to double*
%10 = load double* %n, align 8, !tbaa !19
store double %10, double* %nlimit, align 8, !tbaa !19
br label %if.end35
if.else30: ; preds = %if.else
%call31 = call i32 @luaV_tonumber_(%struct.TValue* %add.ptr5, double* %nlimit) #2
%phitmp = icmp eq i32 %call31, 0
br i1 %phitmp, label %if.then33, label %if.end35
if.then33: ; preds = %if.else30
%call34 = call i32 bitcast (i32 (...)* @luaG_runerror to i32 (%struct.lua_State*, i8*)*)(%struct.lua_State* %L, i8* getelementptr inbounds ([29 x i8]* @.str1, i32 0, i32 0)) #2
br label %if.end35
if.end35: ; preds = %if.else30, %if.then28, %if.then33
%11 = load double* %nlimit, align 8, !tbaa !19
%n37 = bitcast %struct.TValue* %add.ptr5 to double*
store double %11, double* %n37, align 8, !tbaa !19
store i32 3, i32* %tt_25, align 4, !tbaa !15
%12 = load i32* %tt_7, align 4, !tbaa !15
%cmp40 = icmp eq i32 %12, 3
br i1 %cmp40, label %if.then42, label %if.else45
if.then42: ; preds = %if.end35
%n44 = bitcast %struct.TValue* %add.ptr6 to double*
%13 = load double* %n44, align 8, !tbaa !19
store double %13, double* %nstep, align 8, !tbaa !19
br label %if.end51
if.else45: ; preds = %if.end35
%call46 = call i32 @luaV_tonumber_(%struct.TValue* %add.ptr6, double* %nstep) #2
%phitmp104 = icmp eq i32 %call46, 0
br i1 %phitmp104, label %if.then49, label %if.end51
if.then49: ; preds = %if.else45
%call50 = call i32 bitcast (i32 (...)* @luaG_runerror to i32 (%struct.lua_State*, i8*)*)(%struct.lua_State* %L, i8* getelementptr inbounds ([28 x i8]* @.str2, i32 0, i32 0)) #2
br label %if.end51
if.end51: ; preds = %if.else45, %if.then42, %if.then49
%14 = load double* %nstep, align 8, !tbaa !19
%n53 = bitcast %struct.TValue* %add.ptr6 to double*
store double %14, double* %n53, align 8, !tbaa !19
store i32 3, i32* %tt_7, align 4, !tbaa !15
%15 = load i32* %tt_, align 4, !tbaa !15
%cmp56 = icmp eq i32 %15, 3
br i1 %cmp56, label %if.then58, label %if.else61
if.then58: ; preds = %if.end51
%n60 = bitcast %struct.TValue* %add.ptr to double*
%16 = load double* %n60, align 8, !tbaa !19
store double %16, double* %ninit, align 8, !tbaa !19
br label %if.end67
if.else61: ; preds = %if.end51
%call62 = call i32 @luaV_tonumber_(%struct.TValue* %add.ptr, double* %ninit) #2
%phitmp105 = icmp eq i32 %call62, 0
br i1 %phitmp105, label %if.then65, label %if.end67
if.then65: ; preds = %if.else61
%call66 = call i32 bitcast (i32 (...)* @luaG_runerror to i32 (%struct.lua_State*, i8*)*)(%struct.lua_State* %L, i8* getelementptr inbounds ([37 x i8]* @.str3, i32 0, i32 0)) #2
br label %if.end67
if.end67: ; preds = %if.else61, %if.then58, %if.then65
%17 = load double* %ninit, align 8, !tbaa !19
%18 = load double* %nstep, align 8, !tbaa !19
%sub68 = fsub double %17, %18
%n70 = bitcast %struct.TValue* %add.ptr to double*
store double %sub68, double* %n70, align 8, !tbaa !19
br label %label_forloop
label_forloop: ; preds = %cond.end, %if.end67
%storemerge = phi i32 [ 3, %if.end67 ], [ 19, %cond.end ]
store i32 %storemerge, i32* %tt_, align 4, !tbaa !15
ret void
}
declare i32 @forlimit(%struct.TValue*, i64*, i64, i32*) #1
declare i32 @luaV_tonumber_(%struct.TValue*, double*) #1
declare i32 @luaG_runerror(...) #1
attributes #0 = { nounwind "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #1 = { "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #2 = { nounwind }
!llvm.ident = !{!0}
!0 = metadata !{metadata !"clang version 3.6.0 (trunk)"}
!1 = metadata !{metadata !2, metadata !4, i64 4}
!2 = metadata !{metadata !"GCObject", metadata !3, i64 0, metadata !4, i64 4, metadata !4, i64 5}
!3 = metadata !{metadata !"any pointer", metadata !4, i64 0}
!4 = metadata !{metadata !"omnipotent char", metadata !5, i64 0}
!5 = metadata !{metadata !"Simple C/C++ TBAA"}
!6 = metadata !{metadata !7, metadata !3, i64 16}
!7 = metadata !{metadata !"lua_State", metadata !3, i64 0, metadata !4, i64 4, metadata !4, i64 5, metadata !4, i64 6, metadata !3, i64 8, metadata !3, i64 12, metadata !3, i64 16, metadata !3, i64 20, metadata !3, i64 24, metadata !3, i64 28, metadata !3, i64 32, metadata !3, i64 36, metadata !3, i64 40, metadata !3, i64 44, metadata !8, i64 48, metadata !3, i64 104, metadata !9, i64 112, metadata !11, i64 120, metadata !11, i64 124, metadata !11, i64 128, metadata !10, i64 132, metadata !10, i64 134, metadata !4, i64 136, metadata !4, i64 137}
!8 = metadata !{metadata !"CallInfo", metadata !3, i64 0, metadata !3, i64 4, metadata !3, i64 8, metadata !3, i64 12, metadata !4, i64 16, metadata !9, i64 40, metadata !10, i64 48, metadata !4, i64 50}
!9 = metadata !{metadata !"long long", metadata !4, i64 0}
!10 = metadata !{metadata !"short", metadata !4, i64 0}
!11 = metadata !{metadata !"int", metadata !4, i64 0}
!12 = metadata !{metadata !13, metadata !3, i64 16}
!13 = metadata !{metadata !"CallInfoLua", metadata !3, i64 0, metadata !3, i64 4, metadata !3, i64 8, metadata !3, i64 12, metadata !14, i64 16, metadata !9, i64 32, metadata !10, i64 40, metadata !4, i64 42}
!14 = metadata !{metadata !"CallInfoL", metadata !3, i64 0, metadata !3, i64 4, metadata !9, i64 8}
!15 = metadata !{metadata !16, metadata !11, i64 8}
!16 = metadata !{metadata !"TValue", metadata !4, i64 0, metadata !11, i64 8}
!17 = metadata !{metadata !9, metadata !9, i64 0}
!18 = metadata !{metadata !11, metadata !11, i64 0}
!19 = metadata !{metadata !20, metadata !20, i64 0}
!20 = metadata !{metadata !"double", metadata !4, i64 0}

@ -55,4 +55,7 @@ LUAI_FUNC lua_Integer luaV_mod (lua_State *L, lua_Integer x, lua_Integer y);
LUAI_FUNC lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y);
LUAI_FUNC void luaV_objlen (lua_State *L, StkId ra, const TValue *rb);
LUAI_FUNC int luaV_forlimit(const TValue *obj, lua_Integer *p, lua_Integer step,
int *stopnow);
#endif

@ -137,7 +137,7 @@ int luaV_tointeger_ (const TValue *obj, lua_Integer *p) {
** the extreme case when the initial value is LUA_MININTEGER, in which
** case the LUA_MININTEGER limit would still run the loop once.
*/
static int forlimit (const TValue *obj, lua_Integer *p, lua_Integer step,
int luaV_forlimit (const TValue *obj, lua_Integer *p, lua_Integer step,
int *stopnow) {
*stopnow = 0; /* usually, let loops run */
if (!tointeger_aux(obj, p, (step < 0 ? 2 : 1))) { /* not fit in integer? */
@ -1112,7 +1112,7 @@ newframe: /* reentry point when frame changes (call/return) */
lua_Integer ilimit;
int stopnow;
if (ttisinteger(init) && ttisinteger(pstep) &&
forlimit(plimit, &ilimit, ivalue(pstep), &stopnow)) {
luaV_forlimit(plimit, &ilimit, ivalue(pstep), &stopnow)) {
/* all values are integer */
lua_Integer initv = (stopnow ? 0 : ivalue(init));
setivalue(plimit, ilimit);

@ -64,6 +64,7 @@ struct LuaLLVMTypes {
llvm::Type *lua_NumberT;
llvm::Type *lua_IntegerT;
llvm::PointerType *plua_IntegerT;
llvm::Type *lua_UnsignedT;
llvm::Type *lua_KContextT;
@ -87,6 +88,7 @@ struct LuaLLVMTypes {
llvm::Type *C_pcharT;
llvm::Type *C_intT;
llvm::Type *C_pintT;
llvm::StructType *lua_StateT;
llvm::PointerType *plua_StateT;
@ -169,6 +171,7 @@ struct LuaLLVMTypes {
llvm::FunctionType *luaV_lessthanT;
llvm::FunctionType *luaV_lessequalT;
llvm::FunctionType *luaG_runerrorT;
llvm::FunctionType *luaV_forlimitT;
std::array<llvm::Constant *, 21> kInt;
@ -212,6 +215,7 @@ LuaLLVMTypes::LuaLLVMTypes(llvm::LLVMContext &context) : mdbuilder(context) {
static_assert(std::is_integral<lua_Integer>::value,
"lua_Integer is not an integer type");
lua_IntegerT = llvm::Type::getIntNTy(context, sizeof(lua_Integer) * 8);
plua_IntegerT = llvm::PointerType::get(lua_IntegerT, 0);
static_assert(sizeof(lua_Integer) == sizeof(lua_Unsigned),
"lua_Integer and lua_Unsigned are of different size");
@ -222,6 +226,7 @@ LuaLLVMTypes::LuaLLVMTypes(llvm::LLVMContext &context) : mdbuilder(context) {
C_ptrdiff_t = llvm::Type::getIntNTy(context, sizeof(ptrdiff_t) * 8);
C_int64_t = llvm::Type::getIntNTy(context, sizeof(int64_t) * 8);
C_intT = llvm::Type::getIntNTy(context, sizeof(int) * 8);
C_pintT = llvm::PointerType::get(C_intT, 0);
static_assert(sizeof(size_t) == sizeof(lu_mem),
"lu_mem size is not same as size_t");
@ -819,6 +824,13 @@ LuaLLVMTypes::LuaLLVMTypes(llvm::LLVMContext &context) : mdbuilder(context) {
elements.push_back(C_pcharT);
luaG_runerrorT = llvm::FunctionType::get(llvm::Type::getVoidTy(context), elements, true);
elements.clear();
elements.push_back(pTValueT);
elements.push_back(plua_IntegerT);
elements.push_back(lua_IntegerT);
elements.push_back(C_pintT);
luaV_forlimitT = llvm::FunctionType::get(C_intT, elements, false);
for (int j = 0; j < kInt.size(); j++)
kInt[j] = llvm::ConstantInt::get(C_intT, j);
@ -1288,6 +1300,7 @@ struct RaviFunctionDef {
llvm::Constant *luaV_lessthanF;
llvm::Constant *luaV_lessequalF;
llvm::Constant *luaG_runerrorF;
llvm::Constant *luaV_forlimitF;
// Jump targets in the function
std::vector<llvm::BasicBlock *> jmp_targets;
@ -1484,11 +1497,25 @@ void RaviCodeGenerator::emit_FORPREP(RaviFunctionDef *def, llvm::Value *L_ci,
// Load pointer to k
llvm::Value *k_ptr = def->k_ptr;
llvm::Value *init = A == 0 ? base_ptr : emit_array_get(def, base_ptr, A);
llvm::Value *ilimit = def->builder->CreateAlloca(def->types->lua_IntegerT, nullptr, "ilimit");
llvm::Value *stopnow = def->builder->CreateAlloca(def->types->C_intT, nullptr, "stopnow");
llvm::Value *nlimit = def->builder->CreateAlloca(def->types->lua_NumberT, nullptr, "nlimit");
llvm::Value *ninit = def->builder->CreateAlloca(def->types->lua_NumberT, nullptr, "ninit");
llvm::Value *nstep = def->builder->CreateAlloca(def->types->lua_IntegerT, nullptr, "nstep");
llvm::Value *init = A == 0 ? base_ptr : emit_array_get(def, base_ptr, A);
llvm::Value *plimit = emit_array_get(def, base_ptr, A + 1);
llvm::Value *pstep = emit_array_get(def, base_ptr, A + 2);
llvm::Value *tt = emit_gep(def, "tt_", init, 0, 1);
llvm::Instruction *tt_i = def->builder->CreateLoad(tt);
tt_i->setMetadata(llvm::LLVMContext::MD_tbaa, def->types->tbaa_TValue_ttT);
llvm::Value *cmp1 = def->builder->CreateICmpEQ(tt_i, def->types->kInt[LUA_TNUMINT]);
llvm::Value *tt2 = emit_gep(def, "tt_", pstep, 0, 1);
llvm::Instruction *tt_j = def->builder->CreateLoad(tt2);
tt_j->setMetadata(llvm::LLVMContext::MD_tbaa, def->types->tbaa_TValue_ttT);
llvm::Value *icmp2 = def->builder->CreateICmpEQ(tt_j, def->types->kInt[LUA_TNUMINT]);
}
void RaviCodeGenerator::emit_LOADK(RaviFunctionDef *def, llvm::Value *L_ci,
@ -1836,6 +1863,9 @@ void RaviCodeGenerator::emit_extern_declarations(RaviFunctionDef *def) {
def->types->luaV_lessequalT, &luaV_lessequal, "luaV_lessequal");
def->luaG_runerrorF = def->raviF->addExternFunction(
def->types->luaG_runerrorT, &luaG_runerror, "luaG_runerror");
def->luaV_forlimitF = def->raviF->addExternFunction(
def->types->luaV_forlimitT, &luaV_forlimit, "luaV_forlimit");
}
#define RA(i) (base + GETARG_A(i))

Loading…
Cancel
Save