forloop and move

Dibyendu Majumdar 9 years ago
parent b470a2a847
commit cbf3a6c665

@ -69,7 +69,7 @@ set (LUA_CORE_SRCS src/lapi.c src/lcode.c src/lctype.c src/ldebug.c src/ldo.c sr
src/lparser.c src/lstate.c src/lstring.c src/ltable.c src/ltm.c src/lundump.c
src/lvm.c src/lzio.c src/ravijit.cpp src/ravi_llvmtypes.cpp
src/ravi_llvmcodegen.cpp src/ravi_llvmforprep.cpp src/ravi_llvmcomp.cpp
src/ravi_llvmreturn.cpp src/ravi_llvmload.cpp)
src/ravi_llvmreturn.cpp src/ravi_llvmload.cpp src/ravi_llvmforloop.cpp)
# define the lua lib source files
set (LUA_LIB_SRCS src/lauxlib.c src/lbaselib.c src/lbitlib.c src/lcorolib.c src/ldblib.c src/liolib.c
src/lmathlib.c src/loslib.c src/ltablib.c src/lstrlib.c src/loadlib.c src/linit.c src/lutf8lib.c)

@ -0,0 +1,385 @@
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, *rd;
int A;
ci = L->ci;
base = ci->l.base;
cl = (struct LClosure *)(ci->func->value_.gc);
k = cl->p->k;
label_loopbody:
printf("dummy");
#define LUA_NUMINT (3 | 1 << 4)
#define LUA_NUMFLT (3)
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 */
// }
//}
A = 1;
ra = base + A;
if (ra->tt_ == LUA_NUMINT) {
rb = base + (A + 2);
long long int step = rb->value_.i;
long long int idx = ra->value_.i + step;
rc = base + (A + 1);
long long int limit = rc->value_.i;
if ((0 < step) ? (idx <= limit) : (limit <= idx)) {
ra->value_.i = idx;
ra->tt_ = LUA_NUMINT;
rd = base + (A + 3);
rd->value_.i = idx;
rd->tt_ = LUA_NUMINT;
goto label_loopbody;
}
}
else {
rb = base + (A + 2);
double step = rb->value_.n;
double idx = ra->value_.n + step;
rc = base + (A + 1);
double limit = rc->value_.n;
if ((0 < step) ? (idx <= limit) : (limit <= idx)) {
ra->value_.n = idx;
ra->tt_ = LUA_NUMFLT;
rd = base + (A + 3);
rd->value_.n = idx;
rd->tt_ = LUA_NUMFLT;
goto label_loopbody;
}
}
}

@ -0,0 +1,147 @@
; ModuleID = 'lua_op_forloop.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 [6 x i8] c"dummy\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) #1
ret void
}
; Function Attrs: nounwind
declare i32 @printf(i8* nocapture readonly, ...) #0
; Function Attrs: nounwind
define void @test1(%struct.lua_State* nocapture readonly %L) #0 {
entry:
%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
%tt_ = getelementptr inbounds %struct.TValue* %1, i32 1, i32 1
%value_5 = getelementptr inbounds %struct.TValue* %1, i32 3, i32 0
%i = getelementptr inbounds %union.Value* %value_5, i32 0, i32 0
%i7 = getelementptr inbounds %struct.TValue* %add.ptr, i32 0, i32 0, i32 0
%i12 = getelementptr inbounds %struct.TValue* %1, i32 2, i32 0, i32 0
%i23 = getelementptr inbounds %struct.TValue* %1, i32 4, i32 0, i32 0
%tt_24 = getelementptr inbounds %struct.TValue* %1, i32 4, i32 1
%n = bitcast %union.Value* %value_5 to double*
%n31 = bitcast %struct.TValue* %add.ptr to double*
%value_36 = getelementptr inbounds %struct.TValue* %1, i32 2, i32 0
%n37 = bitcast %union.Value* %value_36 to double*
%add.ptr48 = getelementptr inbounds %struct.TValue* %1, i32 4
%n50 = bitcast %struct.TValue* %add.ptr48 to double*
br label %label_loopbody
label_loopbody: ; preds = %label_loopbody.backedge, %entry
%call = tail call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([6 x i8]* @.str1, i32 0, i32 0)) #1
%2 = load i32* %tt_, align 4, !tbaa !15
%cmp = icmp eq i32 %2, 19
br i1 %cmp, label %if.then, label %if.else
if.then: ; preds = %label_loopbody
%3 = load i64* %i, align 8, !tbaa !17
%4 = load i64* %i7, align 8, !tbaa !17
%add8 = add nsw i64 %4, %3
%5 = load i64* %i12, align 8, !tbaa !17
%cmp13 = icmp sgt i64 %3, 0
br i1 %cmp13, label %cond.true, label %cond.false
cond.true: ; preds = %if.then
%cmp14 = icmp sgt i64 %add8, %5
br i1 %cmp14, label %if.end53, label %if.then16
cond.false: ; preds = %if.then
%cmp15 = icmp sgt i64 %5, %add8
br i1 %cmp15, label %if.end53, label %if.then16
if.then16: ; preds = %cond.true, %cond.false
store i64 %add8, i64* %i7, align 8, !tbaa !17
store i32 19, i32* %tt_, align 4, !tbaa !15
store i64 %add8, i64* %i23, align 8, !tbaa !17
br label %label_loopbody.backedge
label_loopbody.backedge: ; preds = %if.then16, %if.then43
%storemerge = phi i32 [ 3, %if.then43 ], [ 19, %if.then16 ]
store i32 %storemerge, i32* %tt_24, align 4, !tbaa !15
br label %label_loopbody
if.else: ; preds = %label_loopbody
%6 = load double* %n, align 8, !tbaa !18
%7 = load double* %n31, align 8, !tbaa !18
%add32 = fadd double %6, %7
%8 = load double* %n37, align 8, !tbaa !18
%cmp38 = fcmp ogt double %6, 0.000000e+00
br i1 %cmp38, label %cond.true39, label %cond.false41
cond.true39: ; preds = %if.else
%cmp40 = fcmp ugt double %add32, %8
br i1 %cmp40, label %if.end53, label %if.then43
cond.false41: ; preds = %if.else
%cmp42 = fcmp ugt double %8, %add32
br i1 %cmp42, label %if.end53, label %if.then43
if.then43: ; preds = %cond.true39, %cond.false41
store double %add32, double* %n31, align 8, !tbaa !18
store i32 3, i32* %tt_, align 4, !tbaa !15
store double %add32, double* %n50, align 8, !tbaa !18
br label %label_loopbody.backedge
if.end53: ; preds = %cond.true, %cond.false, %cond.true39, %cond.false41
ret void
}
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 = { 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 !19, metadata !19, i64 0}
!19 = metadata !{metadata !"double", metadata !4, i64 0}

@ -1,7 +1,6 @@
#ifndef RAVI_LLVMCODEGEN_H
#define RAVI_LLVMCODEGEN_H
#include "ravijit.h"
#include "ravillvm.h"
@ -180,7 +179,7 @@ struct LuaLLVMTypes {
// To allow better optimization we need to decorate the
// LLVM Load/Store instructions with type information.
// For this we need to construct tbaa metadata
// For this we need to construct tbaa metadata
llvm::MDBuilder mdbuilder;
llvm::MDNode *tbaa_root;
llvm::MDNode *tbaa_charT;
@ -237,8 +236,7 @@ class RAVI_API RaviJITFunctionImpl : public RaviJITFunction {
void *ptr_;
public:
RaviJITFunctionImpl(RaviJITStateImpl *owner,
llvm::FunctionType *type,
RaviJITFunctionImpl(RaviJITStateImpl *owner, llvm::FunctionType *type,
llvm::GlobalValue::LinkageTypes linkage,
const std::string &name);
virtual ~RaviJITFunctionImpl();
@ -296,9 +294,7 @@ public:
virtual void dump();
virtual llvm::LLVMContext &context() { return context_; }
LuaLLVMTypes *types() const { return types_; }
const std::string& triple() const {
return triple_;
}
const std::string &triple() const { return triple_; }
};
// This structure holds stuff we need when compiling a single
@ -341,7 +337,6 @@ struct RaviFunctionDef {
llvm::Value *Ci_base;
};
// This class is responsible for compiling Lua byte code
// to LLVM IR
class RaviCodeGenerator {
@ -404,7 +399,13 @@ public:
void emit_JMP(RaviFunctionDef *def, int j);
void emit_FORPREP(RaviFunctionDef *def, llvm::Value *L_ci, llvm::Value *proto,
int A, int sBx);
int A, int sBx);
void emit_FORLOOP(RaviFunctionDef *def, llvm::Value *L_ci, llvm::Value *proto,
int A, int sBx);
void emit_MOVE(RaviFunctionDef *def, llvm::Value *L_ci, llvm::Value *proto,
int A, int B);
// Emit code for OP_EQ, OP_LT and OP_LE
// The callee parameter should be luaV_equalobj, luaV_lessthan and
@ -421,8 +422,6 @@ private:
char temp_[31]; // for name
int id_; // for name
};
}
#endif

@ -90,11 +90,9 @@ bool RaviCodeGenerator::canCompile(Proto *p) {
case OP_EQ:
case OP_LT:
case OP_LE:
#if 0
case OP_FORPREP:
case OP_FORLOOP:
case OP_MOVE:
#endif
break;
default:
return false;
@ -274,6 +272,10 @@ void RaviCodeGenerator::compile(lua_State *L, Proto *p) {
int Bx = GETARG_Bx(i);
emit_LOADK(&def, L_ci, proto, A, Bx);
} break;
case OP_MOVE: {
int B = GETARG_B(i);
emit_MOVE(&def, L_ci, proto, A, B);
} break;
case OP_RETURN: {
int B = GETARG_B(i);
emit_RETURN(&def, L_ci, proto, A, B);
@ -309,7 +311,9 @@ void RaviCodeGenerator::compile(lua_State *L, Proto *p) {
emit_FORPREP(&def, L_ci, proto, A, j);
} break;
case OP_FORLOOP: {
assert(false);
int sbx = GETARG_sBx(i);
int j = sbx + pc + 1;
emit_FORLOOP(&def, L_ci, proto, A, j);
} break;
default:
break;

@ -0,0 +1,210 @@
#include "ravi_llvmcodegen.h"
namespace ravi {
void RaviCodeGenerator::emit_FORLOOP(RaviFunctionDef *def, llvm::Value *L_ci,
llvm::Value *proto, int A, int pc) {
// 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 */
// }
//}
// Load pointer to base
llvm::Instruction *base_ptr = def->builder->CreateLoad(def->Ci_base);
base_ptr->setMetadata(llvm::LLVMContext::MD_tbaa,
def->types->tbaa_luaState_ci_baseT);
// TValue *rinit = ra;
// TValue *rlimit = ra + 1;
// TValue *rstep = ra + 2;
// TValue *rvar = ra + 3
llvm::Value *rinit = A == 0 ? base_ptr : emit_array_get(def, base_ptr, A);
llvm::Value *rlimit = emit_array_get(def, base_ptr, A + 1);
llvm::Value *rstep = emit_array_get(def, base_ptr, A + 2);
llvm::Value *rvar = emit_array_get(def, base_ptr, A + 3);
// Obtain pointers to the tt_ field
llvm::Value *rinit_tt_ptr = emit_gep(def, "init.tt.ptr", rinit, 0, 1);
llvm::Value *rlimit_tt_ptr = emit_gep(def, "limit.tt.ptr", rlimit, 0, 1);
llvm::Value *rstep_tt_ptr = emit_gep(def, "step.tt.ptr", rstep, 0, 1);
llvm::Value *rvar_tt_ptr = emit_gep(def, "var.tt.ptr", rvar, 0, 1);
// Obtain pointers to the value.i field
llvm::Value *step_int_ptr = def->builder->CreateBitCast(rstep, def->types->plua_IntegerT, "step.i");
llvm::Value *idx_int_ptr = def->builder->CreateBitCast(rinit, def->types->plua_IntegerT, "init.i");
llvm::Value *limit_int_ptr = def->builder->CreateBitCast(rlimit, def->types->plua_IntegerT, "limit.i");
llvm::Value *var_int_ptr = def->builder->CreateBitCast(rvar, def->types->plua_IntegerT, "var.i");
// Obtain pointers to the value.n field
llvm::Value *step_double_ptr = def->builder->CreateBitCast(rstep, def->types->plua_NumberT, "step.n");
llvm::Value *idx_double_ptr = def->builder->CreateBitCast(rinit, def->types->plua_NumberT, "init.n");
llvm::Value *limit_double_ptr = def->builder->CreateBitCast(rlimit, def->types->plua_NumberT, "limit.n");
llvm::Value *var_double_ptr = def->builder->CreateBitCast(rvar, def->types->plua_NumberT, "var.n");
// Create the done block
llvm::BasicBlock *exit_block = llvm::BasicBlock::Create(def->jitState->context(), "exit_forloop");
// Is index an integer?
llvm::Instruction *rinit_tt = def->builder->CreateLoad(rinit_tt_ptr);
rinit_tt->setMetadata(llvm::LLVMContext::MD_tbaa, def->types->tbaa_TValue_ttT);
llvm::Value *cmp1 =
def->builder->CreateICmpEQ(rinit_tt, def->types->kInt[LUA_TNUMINT], "is.integer");
// Setup if then else branch for integer
llvm::BasicBlock *if_integer = llvm::BasicBlock::Create(
def->jitState->context(), "if.integer", def->f);
llvm::BasicBlock *else_integer =
llvm::BasicBlock::Create(def->jitState->context(), "if.not.integer");
def->builder->CreateCondBr(cmp1, if_integer, else_integer);
def->builder->SetInsertPoint(if_integer);
// INTEGER CASE
// lua_Integer step = ivalue(ra + 2);
llvm::Instruction *step_int_value = def->builder->CreateLoad(step_int_ptr, "step.i");
step_int_value->setMetadata(llvm::LLVMContext::MD_tbaa, def->types->tbaa_TValue_nT);
// lua_Integer idx = ivalue(ra) + step; /* increment index */
llvm::Instruction *idx_int_value = def->builder->CreateLoad(idx_int_ptr, "init.i");
idx_int_value->setMetadata(llvm::LLVMContext::MD_tbaa, def->types->tbaa_TValue_nT);
llvm::Value *new_idx = def->builder->CreateAdd(step_int_value, idx_int_value, "next.idx", false, true);
// lua_Integer limit = ivalue(ra + 1);
llvm::Instruction *limit_int_value = def->builder->CreateLoad(limit_int_ptr, "limit.i");
limit_int_value->setMetadata(llvm::LLVMContext::MD_tbaa, def->types->tbaa_TValue_nT);
// step > 0?
llvm::Value *step_gt_zero = def->builder->CreateICmpSGT(step_int_value, def->types->kluaInteger[0], "step.gt.zero");
llvm::BasicBlock *step_gt_zero_true = llvm::BasicBlock::Create(
def->jitState->context(), "step.gt.zero.true", def->f);
llvm::BasicBlock *step_gt_zero_false =
llvm::BasicBlock::Create(def->jitState->context(), "step.gt.zero.false");
def->builder->CreateCondBr(step_gt_zero, step_gt_zero_true, step_gt_zero_false);
def->builder->SetInsertPoint(step_gt_zero_true);
// idx > limit?
llvm::Value *new_idx_gt_limit = def->builder->CreateICmpSGT(new_idx, limit_int_value, "idx.gt.limit");
// If idx > limit we are done
llvm::BasicBlock *update_block = llvm::BasicBlock::Create(
def->jitState->context(), "update");
def->builder->CreateCondBr(new_idx_gt_limit, exit_block, update_block);
def->f->getBasicBlockList().push_back(step_gt_zero_false);
def->builder->SetInsertPoint(step_gt_zero_false);
// limit > idx?
llvm::Value *limit_gt_idx = def->builder->CreateICmpSGT(limit_int_value, new_idx, "limit.gt.idx");
// If limit > idx we are done
def->builder->CreateCondBr(limit_gt_idx, exit_block, update_block);
// Merge into update block
def->f->getBasicBlockList().push_back(update_block);
def->builder->SetInsertPoint(update_block);
// setivalue(ra, idx); /* update internal index... */
llvm::Instruction *idx_store = def->builder->CreateStore(new_idx, idx_int_ptr);
idx_store->setMetadata(llvm::LLVMContext::MD_tbaa, def->types->tbaa_TValue_nT);
llvm::Instruction *idx_tt_store = def->builder->CreateStore(def->types->kInt[LUA_TNUMINT], rinit_tt_ptr);
idx_tt_store->setMetadata(llvm::LLVMContext::MD_tbaa, def->types->tbaa_TValue_ttT);
// setivalue(ra + 3, idx); /* ...and external index */
idx_store = def->builder->CreateStore(new_idx, var_int_ptr);
idx_store->setMetadata(llvm::LLVMContext::MD_tbaa, def->types->tbaa_TValue_nT);
idx_tt_store = def->builder->CreateStore(def->types->kInt[LUA_TNUMINT], rvar_tt_ptr);
idx_tt_store->setMetadata(llvm::LLVMContext::MD_tbaa, def->types->tbaa_TValue_ttT);
// ci->u.l.savedpc += GETARG_sBx(i); /* jump back */
def->builder->CreateBr(def->jmp_targets[pc]);
// FLOATING CASE
def->f->getBasicBlockList().push_back(else_integer);
def->builder->SetInsertPoint(else_integer);
// lua_Number step = fltvalue(ra + 2);
llvm::Instruction *step_double_value = def->builder->CreateLoad(step_double_ptr, "step.n");
step_double_value->setMetadata(llvm::LLVMContext::MD_tbaa, def->types->tbaa_TValue_nT);
// lua_Number idx = luai_numadd(L, fltvalue(ra), step); /* inc. index */
llvm::Instruction *idx_double_value = def->builder->CreateLoad(idx_double_ptr, "init.n");
idx_double_value->setMetadata(llvm::LLVMContext::MD_tbaa, def->types->tbaa_TValue_nT);
new_idx = def->builder->CreateFAdd(step_double_value, idx_double_value, "next.idx");
// lua_Number limit = fltvalue(ra + 1);
llvm::Instruction *limit_double_value = def->builder->CreateLoad(limit_double_ptr, "limit.n");
limit_double_value->setMetadata(llvm::LLVMContext::MD_tbaa, def->types->tbaa_TValue_nT);
// step > 0?
step_gt_zero = def->builder->CreateFCmpOGT(step_double_value, llvm::ConstantFP::get(def->types->lua_NumberT, 0.0), "step.gt.zero");
step_gt_zero_true = llvm::BasicBlock::Create(
def->jitState->context(), "step.gt.zero.true", def->f);
step_gt_zero_false =
llvm::BasicBlock::Create(def->jitState->context(), "step.gt.zero.false");
def->builder->CreateCondBr(step_gt_zero, step_gt_zero_true, step_gt_zero_false);
def->builder->SetInsertPoint(step_gt_zero_true);
// idx > limit?
new_idx_gt_limit = def->builder->CreateFCmpOGT(new_idx, limit_double_value, "idx.gt.limit");
// If idx > limit we are done
update_block = llvm::BasicBlock::Create(
def->jitState->context(), "update");
def->builder->CreateCondBr(new_idx_gt_limit, exit_block, update_block);
def->f->getBasicBlockList().push_back(step_gt_zero_false);
def->builder->SetInsertPoint(step_gt_zero_false);
// limit > idx?
limit_gt_idx = def->builder->CreateFCmpOGT(limit_double_value, new_idx, "limit.gt.idx");
// If limit > idx we are done
def->builder->CreateCondBr(limit_gt_idx, exit_block, update_block);
// Merge into update block
def->f->getBasicBlockList().push_back(update_block);
def->builder->SetInsertPoint(update_block);
// setfltvalue(ra, idx); /* update internal index... */
idx_store = def->builder->CreateStore(new_idx, idx_double_ptr);
idx_store->setMetadata(llvm::LLVMContext::MD_tbaa, def->types->tbaa_TValue_nT);
idx_tt_store = def->builder->CreateStore(def->types->kInt[LUA_TNUMFLT], rinit_tt_ptr);
idx_tt_store->setMetadata(llvm::LLVMContext::MD_tbaa, def->types->tbaa_TValue_ttT);
// setfltvalue(ra + 3, idx); /* ...and external index */
idx_store = def->builder->CreateStore(new_idx, var_double_ptr);
idx_store->setMetadata(llvm::LLVMContext::MD_tbaa, def->types->tbaa_TValue_nT);
idx_tt_store = def->builder->CreateStore(def->types->kInt[LUA_TNUMFLT], rvar_tt_ptr);
idx_tt_store->setMetadata(llvm::LLVMContext::MD_tbaa, def->types->tbaa_TValue_ttT);
// ci->u.l.savedpc += GETARG_sBx(i); /* jump back */
def->builder->CreateBr(def->jmp_targets[pc]);
def->f->getBasicBlockList().push_back(exit_block);
def->builder->SetInsertPoint(exit_block);
}
}

@ -404,7 +404,6 @@ void RaviCodeGenerator::emit_FORPREP(RaviFunctionDef *def, llvm::Value *L_ci,
// Done so jump to forloop
def->builder->CreateBr(def->jmp_targets[pc]);
def->f->dump();
assert(false);
}
}

@ -2,6 +2,53 @@
namespace ravi {
void RaviCodeGenerator::emit_MOVE(RaviFunctionDef *def, llvm::Value *L_ci,
llvm::Value *proto, int A, int B) {
// case OP_MOVE: {
// setobjs2s(L, ra, RB(i));
//} break;
// Load pointer to base
llvm::Instruction *base_ptr = def->builder->CreateLoad(def->Ci_base);
base_ptr->setMetadata(llvm::LLVMContext::MD_tbaa,
def->types->tbaa_luaState_ci_baseT);
llvm::Value *src;
llvm::Value *dest;
lua_assert(A != B);
if (A == 0) {
// If A is 0 we can use the base pointer which is &base[0]
dest = base_ptr;
} else {
// emit &base[A]
dest = emit_array_get(def, base_ptr, A);
}
if (B == 0) {
// If Bx is 0 we can use the base pointer which is &k[0]
src = base_ptr;
} else {
// emit &base[B]
src = emit_array_get(def, base_ptr, B);
}
// destvalue->value->i = srcvalue->value->i;
llvm::Value *srcvalue = emit_gep(def, "srcvalue", src, 0, 0, 0);
llvm::Value *destvalue = emit_gep(def, "destvalue", dest, 0, 0, 0);
llvm::Instruction *store =
def->builder->CreateStore(def->builder->CreateLoad(srcvalue), destvalue);
store->setMetadata(llvm::LLVMContext::MD_tbaa, def->types->tbaa_TValue_nT);
// destvalue->type = srcvalue->type
llvm::Value *srctype = emit_gep(def, "srctype", src, 0, 1);
llvm::Value *desttype = emit_gep(def, "desttype", dest, 0, 1);
store =
def->builder->CreateStore(def->builder->CreateLoad(srctype), desttype);
store->setMetadata(llvm::LLVMContext::MD_tbaa, def->types->tbaa_TValue_ttT);
}
void RaviCodeGenerator::emit_LOADK(RaviFunctionDef *def, llvm::Value *L_ci,
llvm::Value *proto, int A, int Bx) {
// case OP_LOADK: {

@ -161,7 +161,7 @@ void *RaviJITFunctionImpl::compile() {
// Run the FPM on this function
FPM->run(*function_);
//module_->dump();
module_->dump();
// We don't need this anymore
delete FPM;

@ -67,7 +67,7 @@ static int test_luacompexec1(const char *code, int expected)
int main(int argc, const char *argv[])
{
int failures = 0;
failures += test_luacompexec1("local function x(); local j = 0; for i=1,5 do; j = i; end; return j; end; local y = x(); print(y); return y", 5);
failures += test_luacompexec1("local function x(); local j = 0; for i=1,1000000000 do; j = i; end; return j; end; local y = x(); print(y); return y", 1000000000);
failures += test_luacompexec1("local function x(); local a=5; return 1004,2; end; local y; y = x(); print(y); return y", 1004);
failures += test_luacompexec1("local function x(); if 1 == 2 then; return 5.0; end; return 1.0; end; local z = x(); print(z); return z", 1);
failures += test_luacompexec1("local function x(y); if y == 1 then; return 1.0; elseif y == 5 then; return 2.0; else; return 3.0; end; end; local z = x(5); print(z); return z", 2);

Loading…
Cancel
Save