PageRenderTime 68ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/src/luaopen_clang_llvm.cpp

http://luaclang.googlecode.com/
C++ | 1529 lines | 1162 code | 60 blank | 307 comment | 136 complexity | 61f613e634a4d8242a85584e12a05e14 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. //#include "clang/Analysis/PathDiagnostic.h"
  2. #include "clang/AST/ASTContext.h"
  3. #include "clang/AST/Decl.h"
  4. #include "clang/Basic/Builtins.h"
  5. #include "clang/Basic/Diagnostic.h"
  6. #include "clang/Basic/TargetInfo.h"
  7. #include "clang/Basic/SourceManager.h"
  8. #include "clang/Basic/FileManager.h"
  9. #include "clang/CodeGen/ModuleBuilder.h"
  10. #include "clang/Driver/Driver.h"
  11. //#include "clang/Frontend/CompileOptions.h"
  12. //#include "clang/Frontend/InitHeaderSearch.h"
  13. //#include "clang/Frontend/InitPreprocessor.h"
  14. #include "clang/Frontend/TextDiagnosticBuffer.h"
  15. #include "clang/Frontend/TextDiagnosticPrinter.h"
  16. #include "clang/Lex/HeaderSearch.h"
  17. #include "clang/Lex/Preprocessor.h"
  18. //#include "clang/Sema/ParseAST.h"
  19. #include "llvm/ADT/OwningPtr.h"
  20. #include "llvm/ADT/SmallPtrSet.h"
  21. #include "llvm/ADT/SmallString.h"
  22. #include "llvm/ADT/StringExtras.h"
  23. #include "llvm/Analysis/Verifier.h"
  24. #include "llvm/Bitcode/ReaderWriter.h"
  25. #include "llvm/Analysis/Verifier.h"
  26. #include "llvm/Config/config.h"
  27. #include "llvm/DerivedTypes.h"
  28. #include "llvm/ExecutionEngine/ExecutionEngine.h"
  29. #include "llvm/ExecutionEngine/GenericValue.h"
  30. #include "llvm/ExecutionEngine/JIT.h"
  31. #include "llvm/ExecutionEngine/JITEventListener.h"
  32. #include "llvm/Linker.h"
  33. #include "llvm/Module.h"
  34. //#include "llvm/ModuleProvider.h"
  35. #include "llvm/Target/TargetData.h"
  36. //#include "llvm/Target/TargetSelect.h"
  37. #include "llvm/Transforms/Scalar.h"
  38. #include "llvm/Type.h"
  39. #include "llvm/PassManager.h"
  40. #include "llvm/Support/Allocator.h"
  41. #include "llvm/Support/DataTypes.h"
  42. #include "llvm/Support/IRBuilder.h"
  43. #include "llvm/Support/ManagedStatic.h"
  44. #include "llvm/Support/MemoryBuffer.h"
  45. #include "llvm/Support/PassNameParser.h"
  46. #include "llvm/Support/PrettyStackTrace.h"
  47. #include "llvm/Support/raw_ostream.h"
  48. //#include "llvm/System/DynamicLibrary.h"
  49. //#include "llvm/System/Host.h"
  50. //#include "llvm/System/Path.h"
  51. //#include "llvm/System/Signals.h"
  52. #include "llvm/Transforms/IPO.h"
  53. #include "llvm/Transforms/Scalar.h"
  54. #include "llvm/ValueSymbolTable.h"
  55. #include "lua_glue.h"
  56. #if defined(WIN32) || defined(__WINDOWS_MM__)
  57. #define LUA_CLANG_WIN32 1
  58. #ifdef LUA_CLANG_EXPORTS
  59. #define LUA_CLANG_API __declspec(dllexport)
  60. #else
  61. #define LUA_CLANG_API __declspec(dllimport)
  62. #endif
  63. #elif defined( __APPLE__ ) && defined( __MACH__ )
  64. #define LUA_CLANG_OSX 1
  65. #define LUA_CLANG_API extern
  66. #else
  67. #define LUA_CLANG_LINUX 1
  68. #define LUA_CLANG_API extern
  69. #endif
  70. #define ddebug(...)
  71. //#define ddebug(...) printf(__VA_ARGS__)
  72. // used by almost all LLVM types:
  73. template<typename T>
  74. int llvm_print(lua_State * L, T * u) {
  75. std::stringstream s;
  76. u->print(s);
  77. lua_pushfstring(L, "%s", s.str().c_str());
  78. return 1;
  79. }
  80. /*
  81. ModuleProvider
  82. */
  83. #pragma mark ModuleProvider
  84. template<> const char * Glue<ModuleProvider>::usr_name() { return "ModuleProvider"; }
  85. template<> int Glue<ModuleProvider>::usr_tostring(lua_State * L, ModuleProvider * u) {
  86. lua_pushfstring(L, "%s: %s(%p)", usr_name(), u->getModule()->getModuleIdentifier().c_str(), u);
  87. return 1;
  88. }
  89. // retrieve Module from Compiler object instead:
  90. //template<> ModuleProvider * Glue<ModuleProvider>::usr_new(lua_State * L) {
  91. // //Module * m = Glue<Module>::checkto(L, 1);
  92. // //return new ExistingModuleProvider(m);
  93. // const char * modulename = luaL_checkstring(L, 1);
  94. // Module * M = new Module(modulename, getGlobalContext());
  95. // return createModuleProviderFromJIT(L, M);
  96. //}
  97. //template<> void Glue<ModuleProvider>::usr_gc(lua_State * L, ModuleProvider * mp) {
  98. //// printf("~module %p\n", mp);
  99. // std::string err;
  100. // ExecutionEngine * ee = getEE();
  101. // Module * m = mp->getModule();
  102. // if (m) {
  103. // ee->runStaticConstructorsDestructors(m, true);
  104. // ee->clearGlobalMappingsFromModule(m);
  105. //
  106. // // iterate functions and remove from EE:
  107. // for (Module::iterator i = m->begin(), e = m->end(); i != e; ++i) {
  108. // ee->freeMachineCodeForFunction(i);
  109. // }
  110. //
  111. // m->dropAllReferences();
  112. // }
  113. // ee->removeModuleProvider(mp, &err);
  114. // // this isn't safe, since we may be using things created in m:
  115. // //ee->deleteModuleProvider(mp);
  116. //
  117. // printf("removed %s %s\n", m->getModuleIdentifier().data(), err.data());
  118. //// delete m;
  119. //}
  120. //static int moduleprovider_module(lua_State * L) {
  121. // ModuleProvider * mp = Glue<ModuleProvider>::checkto(L, 1);
  122. // Glue<Module>::push(L, mp->getModule());
  123. // return 1;
  124. //}
  125. static int moduleprovider_dump(lua_State * L) {
  126. Module * m = Glue<ModuleProvider>::checkto(L, 1)->getModule();
  127. m->dump();
  128. return 0;
  129. }
  130. int moduleprovider_addTypeName(lua_State * L) {
  131. Module * m = Glue<ModuleProvider>::checkto(L, 1)->getModule();
  132. const Type * ty = Glue<Type>::checkto(L, 2);
  133. const char * name = luaL_checkstring(L, 3);
  134. lua_pushboolean(L, m->addTypeName(name, ty));
  135. return 1;
  136. }
  137. int moduleprovider_getTypeName(lua_State * L) {
  138. Module * m = Glue<ModuleProvider>::checkto(L, 1)->getModule();
  139. const Type * ty = Glue<Type>::checkto(L, 2);
  140. lua_pushstring(L, m->getTypeName(ty).c_str());
  141. return 1;
  142. }
  143. int moduleprovider_getTypeByName(lua_State * L) {
  144. Module * m = Glue<ModuleProvider>::checkto(L, 1)->getModule();
  145. const char * name = luaL_checkstring(L, 2);
  146. const Type * t = m->getTypeByName(name);
  147. if (t == 0)
  148. return luaL_error(L, "Type %s not found", name);
  149. return Glue<Type>::push(L, (Type *)t);
  150. }
  151. // do these via Compiler object instead:
  152. //int moduleprovider_writeBitcodeFile(lua_State * L) {
  153. // Module * m = Glue<ModuleProvider>::checkto(L, 1)->getModule();
  154. // std::ofstream ofile(luaL_checkstring(L, 2), std::ios_base::out | std::ios_base::trunc);
  155. // WriteBitcodeToFile(m, ofile);
  156. // ofile.close();
  157. // return 0;
  158. //}
  159. //static int moduleprovider_link(lua_State * L) {
  160. // std::string err;
  161. // Module * self = Glue<ModuleProvider>::checkto(L, 1)->getModule();
  162. // Module * mod = Glue<ModuleProvider>::checkto(L, 2)->getModule();
  163. // llvm::Linker::LinkModules(self, mod, &err);
  164. // if (err.length())
  165. // luaL_error(L, err.c_str());
  166. // // mod can't be used anymore, as it has been subsumed into self:
  167. // lua_pushnil(L);
  168. // lua_setmetatable(L, 2);
  169. // return 0;
  170. //}
  171. //int moduleprovider_optimize(lua_State * L) {
  172. // Module * M = Glue<ModuleProvider>::checkto(L, 1)->getModule();
  173. // bool DisableOptimizations = lua_toboolean(L, 2);
  174. // bool DisableInline = lua_toboolean(L, 3);
  175. // optimize_module(M, DisableOptimizations, DisableInline);
  176. // return 0;
  177. //}
  178. //int moduleprovider_functions(lua_State * L) {
  179. // Module * M = Glue<ModuleProvider>::checkto(L, 1)->getModule();
  180. // lua_newtable(L);
  181. // for (Module::iterator i = M->begin(), e = M->end(); i != e; ++i) {
  182. // lua_pushstring(L, i->getName().data());
  183. // Glue<Function>::push(L, i);
  184. // lua_rawset(L, -3);
  185. // }
  186. // return 1;
  187. //}
  188. template<> void Glue<ModuleProvider> :: usr_mt(lua_State * L, int mt) {
  189. //lua_pushcfunction(L, moduleprovider_module); lua_setfield(L, -2, "getModule");
  190. lua_pushcfunction(L, moduleprovider_dump); lua_setfield(L, mt, "dump");
  191. lua_pushcfunction(L, moduleprovider_addTypeName); lua_setfield(L, mt, "addTypeName");
  192. lua_pushcfunction(L, moduleprovider_getTypeName); lua_setfield(L, mt, "getTypeName");
  193. lua_pushcfunction(L, moduleprovider_getTypeByName); lua_setfield(L, mt, "getTypeByName");
  194. lua_pushcfunction(L, Glue<Function>::create); lua_setfield(L, mt, "Function");
  195. // get these via Compiler object instead:
  196. //lua_pushcfunction(L, moduleprovider_writeBitcodeFile); lua_setfield(L, mt, "writeBitcodeFile");
  197. //lua_pushcfunction(L, moduleprovider_link); lua_setfield(L, mt, "link");
  198. //lua_pushcfunction(L, moduleprovider_optimize); lua_setfield(L, mt, "optimize");
  199. //lua_pushcfunction(L, moduleprovider_functions); lua_setfield(L, mt, "functions");
  200. //lua_pushcfunction(L, ee_call); lua_setfield(L, mt, "call");
  201. //lua_pushcfunction(L, ee_pushluafunction); lua_setfield(L, mt, "pushluafunction");
  202. }
  203. /*
  204. Type
  205. */
  206. #pragma mark Type
  207. template<> const char * Glue<Type>::usr_name() { return "Type"; }
  208. template<> int Glue<Type>::usr_tostring(lua_State * L, Type * t) { return llvm_print(L, t); }
  209. // type call with no argument is a constant constructor
  210. // type call with an argument is a cast operator
  211. //static int type_call(lua_State * L) {
  212. // const Type * t = Glue<Type>::checkto(L, 1);
  213. // if (!lua_isuserdata(L, 2)) {
  214. // // constant initializer:
  215. // switch (t->getTypeID()) {
  216. // case Type::VoidTyID:
  217. // return Glue<Constant>::push(L, ConstantPointerNull::get((const PointerType *)Type::getVoidTy(getGlobalContext())));
  218. // case Type::FloatTyID:
  219. // case Type::DoubleTyID:
  220. // return Glue<Constant>::push(L, ConstantFP::get(t, luaL_optnumber(L, 2, 0.)));
  221. // case Type::IntegerTyID:
  222. // return Glue<Constant>::push(L, ConstantInt::get(t, luaL_optinteger(L, 2, 0)));
  223. // }
  224. // return luaL_error(L, "Type cannot be constructed as a constant");
  225. // } else {
  226. // // cast:
  227. // Value * v = Glue<Value>::checkto(L, 2);
  228. // const Type * t2 = v->getType();
  229. // IRBuilder<> * b = getLModule(L)->getBuilder();
  230. //
  231. // // can't cast void:
  232. // if (t->getTypeID() == Type::VoidTyID || t2->getTypeID() == Type::VoidTyID)
  233. // return luaL_error(L, "Cannot cast to/from Void");
  234. //
  235. // // ptr to ptr
  236. // if (t->getTypeID() == Type::PointerTyID && t2->getTypeID() == Type::PointerTyID) {
  237. // return Glue<Value>::push(L, b->CreateBitCast(v, t, "cast"));
  238. // }
  239. //
  240. // // int to float:
  241. // if (t->isInteger() && t2->isFloatingPoint())
  242. // return Glue<Value>::push(L, b->CreateFPToSI(v, t, "intcast"));
  243. // // float to int
  244. // if (t->isFloatingPoint() && t2->isInteger())
  245. // return Glue<Value>::push(L, b->CreateSIToFP(v, t, "floatcast"));
  246. //
  247. // // int to int
  248. // if (t->isInteger() == t2->isInteger()) {
  249. // const IntegerType * it = (IntegerType *)t;
  250. // const IntegerType * it2 = (IntegerType *)t2;
  251. // if (it->getBitWidth() > it2->getBitWidth()) {
  252. // return Glue<Value>::push(L, b->CreateZExt(v, it, "trunc"));
  253. // } else if (it->getBitWidth() < it2->getBitWidth()) {
  254. // return Glue<Value>::push(L, b->CreateTrunc(v, it, "trunc"));
  255. // } else {
  256. // return 1; // no cast required
  257. // }
  258. // }
  259. //
  260. // return luaL_error(L, "unrecognized cast");
  261. // }
  262. //}
  263. int type_id(lua_State * L) {
  264. const Type * t = Glue<Type>::checkto(L, 1);
  265. lua_pushinteger(L, t->getTypeID());
  266. return 1;
  267. }
  268. int type_isinteger(lua_State * L) {
  269. const Type * t = Glue<Type>::checkto(L, 1);
  270. lua_pushboolean(L, t->isInteger());
  271. return 1;
  272. }
  273. int type_isfloatingpoint(lua_State * L) {
  274. const Type * t = Glue<Type>::checkto(L, 1);
  275. lua_pushboolean(L, t->isFloatingPoint());
  276. return 1;
  277. }
  278. int type_isabstract(lua_State * L) {
  279. const Type * t = Glue<Type>::checkto(L, 1);
  280. lua_pushboolean(L, t->isAbstract());
  281. return 1;
  282. }
  283. int type_issized(lua_State * L) {
  284. const Type * t = Glue<Type>::checkto(L, 1);
  285. lua_pushboolean(L, t->isSized());
  286. return 1;
  287. }
  288. int type_isValidReturnType(lua_State * L) {
  289. const Type * t = Glue<Type>::checkto(L);
  290. lua_pushboolean(L, FunctionType::isValidReturnType(t));
  291. return 1;
  292. }
  293. //int type_sizeABI(lua_State * L) {
  294. // const Type * t = Glue<Type>::checkto(L, 1);
  295. // ExecutionEngine * ee = LModule::getEE();
  296. // const TargetData * td = ee->getTargetData();
  297. // /// getTypeSizeInBits - Return the number of bits necessary to hold the
  298. // /// specified type. For example, returns 36 for i36 and 80 for x86_fp80.
  299. // lua_pushinteger(L, td->getABITypeSize(t));
  300. // return 1;
  301. //}
  302. int type_sizeinbits(lua_State * L) {
  303. const Type * t = Glue<Type>::checkto(L, 1);
  304. if (EE == 0) luaL_error(L, "No Execution Engine");
  305. const TargetData * td = EE->getTargetData();
  306. /// getTypeSizeInBits - Return the number of bits necessary to hold the
  307. /// specified type. For example, returns 36 for i36 and 80 for x86_fp80.
  308. lua_pushinteger(L, td->getTypeSizeInBits(t));
  309. return 1;
  310. }
  311. static int type_pointer(lua_State * L) {
  312. const Type * t = Glue<Type>::checkto(L, 1);
  313. int addressSpace = luaL_optinteger(L, 2, 0);
  314. if (t->getTypeID() == Type::VoidTyID) {
  315. // special case for void *:
  316. return Glue<PointerType>::push(L, PointerType::get(Type::getInt8Ty(getGlobalContext()), addressSpace));
  317. }
  318. return Glue<PointerType>::push(L, PointerType::get(t, addressSpace));
  319. }
  320. static int type_eq(lua_State * L) {
  321. //lua::dump(L, "eq");
  322. const Type * a = Glue<Type>::checkto(L, 1);
  323. const Type * b = Glue<Type>::checkto(L, 2);
  324. //ddebug("%p %p\n", a, b);
  325. lua_pushboolean(L, a == b);
  326. return 1;
  327. }
  328. int type_modname(lua_State * L) {
  329. Module * M = Glue<ModuleProvider>::checkto(L, 1)->getModule();
  330. const Type * t = Glue<Type>::checkto(L, 2);
  331. lua_pushstring(L, M->getTypeName(t).c_str());
  332. return 1;
  333. }
  334. template<> void Glue<Type>::usr_mt(lua_State * L, int mt) {
  335. //lua_pushcfunction(L, type_call); lua_setfield(L, -2, "__call");
  336. lua_pushcfunction(L, type_eq); lua_setfield(L, mt, "__eq");
  337. lua_pushcfunction(L, type_pointer); lua_setfield(L, mt, "pointer");
  338. lua_pushcfunction(L, type_pointer); lua_setfield(L, mt, "ptr");
  339. lua_pushcfunction(L, type_modname); lua_setfield(L, mt, "name");
  340. lua_pushcfunction(L, type_isinteger); lua_setfield(L, mt, "isinteger");
  341. lua_pushcfunction(L, type_isfloatingpoint); lua_setfield(L, mt, "isfloatingpoint");
  342. lua_pushcfunction(L, type_isabstract); lua_setfield(L, mt, "isabstract");
  343. lua_pushcfunction(L, type_issized); lua_setfield(L, mt, "issized");
  344. lua_pushcfunction(L, type_sizeinbits); lua_setfield(L, mt, "sizeinbits");
  345. lua_pushcfunction(L, type_isValidReturnType); lua_setfield(L, mt, "isValidReturnType");
  346. //lua_pushcfunction(L, type_sizeABI); lua_setfield(L, mt, "size");
  347. lua_pushcfunction(L, type_id); lua_setfield(L, mt, "id");
  348. //lua_pushcfunction(L, ee_offsetOf); lua_setfield(L, mt, "offsetOf");
  349. Glue<Type>::push(L, (Type *)Type::getVoidTy(getGlobalContext())); lua_setfield(L, mt, "Void");
  350. Glue<Type>::push(L, (Type *)Type::getLabelTy(getGlobalContext())); lua_setfield(L, mt, "Label");
  351. Glue<Type>::push(L, (Type *)Type::getFloatTy(getGlobalContext())); lua_setfield(L, mt, "Float");
  352. Glue<Type>::push(L, (Type *)Type::getDoubleTy(getGlobalContext())); lua_setfield(L, mt, "Double");
  353. Glue<Type>::push(L, (Type *)Type::getInt1Ty(getGlobalContext())); lua_setfield(L, mt, "Int1");
  354. Glue<Type>::push(L, (Type *)Type::getInt8Ty(getGlobalContext())); lua_setfield(L, mt, "Int8");
  355. Glue<Type>::push(L, (Type *)Type::getInt16Ty(getGlobalContext())); lua_setfield(L, mt, "Int16");
  356. Glue<Type>::push(L, (Type *)Type::getInt32Ty(getGlobalContext())); lua_setfield(L, mt, "Int32");
  357. Glue<Type>::push(L, (Type *)Type::getInt64Ty(getGlobalContext())); lua_setfield(L, mt, "Int64");
  358. }
  359. /*
  360. StructType : CompositeType : DerivedType : Type
  361. */
  362. #pragma mark StructType
  363. template<> const char * Glue<StructType>::usr_name() { return "StructType"; }
  364. template<> const char * Glue<StructType>::usr_supername() { return "Type"; }
  365. template<> int Glue<StructType>::usr_tostring(lua_State * L, StructType * t) { return llvm_print<StructType>(L, t); }
  366. template<> StructType * Glue<StructType>::usr_new(lua_State * L) {
  367. std::vector<const Type *> types;
  368. if (lua_type(L, 1) == LUA_TTABLE) {
  369. int ntypes = lua_objlen(L, 1);
  370. for (int i=1; i<= ntypes; i++) {
  371. lua_rawgeti(L, 1, i);
  372. types.push_back(Glue<Type>::checkto(L, -1));
  373. lua_pop(L, 1);
  374. }
  375. }
  376. bool isPacked = false; // this is true for unions, I think??
  377. return StructType::get((getGlobalContext()), types, isPacked);
  378. }
  379. static int structtype_len(lua_State * L) {
  380. StructType * u = Glue<StructType>::checkto(L, 1);
  381. lua_pushinteger(L, u->getNumElements());
  382. return 1;
  383. }
  384. static int structtype_getelementtype(lua_State * L) {
  385. StructType * u = Glue<StructType>::checkto(L, 1);
  386. unsigned int i = luaL_checkinteger(L, 2);
  387. if (i >= u->getNumElements())
  388. return luaL_error(L, "StructType has only %d elements (requested %d)", u->getNumElements(), i);
  389. return Glue<Type>::push(L, (Type *)u->getElementType(i));
  390. }
  391. static int structtype_gettypes(lua_State * L) {
  392. StructType * u = Glue<StructType>::checkto(L, 1);
  393. for (unsigned int i=0; i< u->getNumElements(); i++) {
  394. Glue<Type>::push(L, (Type *)u->getElementType(i));
  395. }
  396. return u->getNumElements();
  397. }
  398. template<> void Glue<StructType>::usr_mt(lua_State * L, int mt) {
  399. lua_pushcfunction(L, structtype_len); lua_setfield(L, mt, "__len");
  400. lua_pushcfunction(L, type_eq); lua_setfield(L, mt, "__eq");
  401. lua_pushcfunction(L, structtype_getelementtype); lua_setfield(L, mt, "type");
  402. lua_pushcfunction(L, structtype_gettypes); lua_setfield(L, mt, "types");
  403. }
  404. /*
  405. SequentialType : CompositeType : DerivedType : Type
  406. */
  407. #pragma mark SequentialType
  408. template<> const char * Glue<SequentialType>::usr_name() { return "SequentialType"; }
  409. template<> const char * Glue<SequentialType>::usr_supername() { return "Type"; }
  410. template<> int Glue<SequentialType>::usr_tostring(lua_State * L, SequentialType * t) { return llvm_print<SequentialType>(L, t); }
  411. static int sequentialtype_getelementtype(lua_State * L) {
  412. SequentialType * u = Glue<SequentialType>::checkto(L, 1);
  413. return Glue<Type>::push(L, (Type *)u->getElementType());
  414. }
  415. template<> void Glue<SequentialType>::usr_mt(lua_State * L, int mt) {
  416. lua_pushcfunction(L, type_eq); lua_setfield(L, mt, "__eq");
  417. lua_pushcfunction(L, sequentialtype_getelementtype); lua_setfield(L, mt, "type");
  418. }
  419. /*
  420. PointerType : SequentialType
  421. */
  422. #pragma mark PointerType
  423. template<> const char * Glue<PointerType>::usr_name() { return "PointerType"; }
  424. template<> const char * Glue<PointerType>::usr_supername() { return "SequentialType"; }
  425. template<> int Glue<PointerType>::usr_tostring(lua_State * L, PointerType * t) { return llvm_print<PointerType>(L, t); }
  426. template<> PointerType * Glue<PointerType>::usr_new(lua_State * L) {
  427. const Type * t = Glue<Type>::checkto(L, 1);
  428. int addressSpace = luaL_optinteger(L, 2, 0);
  429. return PointerType::get(t, addressSpace);
  430. }
  431. template<> void Glue<PointerType>::usr_mt(lua_State * L, int mt) {
  432. lua_pushcfunction(L, type_eq); lua_setfield(L, mt, "__eq");
  433. }
  434. /*
  435. ArrayType : SequentialType
  436. */
  437. #pragma mark ArrayType
  438. template<> const char * Glue<ArrayType>::usr_name() { return "ArrayType"; }
  439. template<> const char * Glue<ArrayType>::usr_supername() { return "SequentialType"; }
  440. template<> int Glue<ArrayType>::usr_tostring(lua_State * L, ArrayType * t) { return llvm_print<ArrayType>(L, t); }
  441. template<> ArrayType * Glue<ArrayType>::usr_new(lua_State * L) {
  442. const Type * t = Glue<Type>::checkto(L, 1);
  443. int len = luaL_optinteger(L, 2, 1);
  444. return ArrayType::get(t, len);
  445. }
  446. static int arraytype_len(lua_State * L) {
  447. ArrayType * u = Glue<ArrayType>::checkto(L, 1);
  448. lua_pushinteger(L, u->getNumElements());
  449. return 1;
  450. }
  451. template<> void Glue<ArrayType>::usr_mt(lua_State * L, int mt) {
  452. lua_pushcfunction(L, arraytype_len); lua_setfield(L, mt, "__len");
  453. lua_pushcfunction(L, type_eq); lua_setfield(L, mt, "__eq");
  454. }
  455. /*
  456. VectorType : SequentialType
  457. */
  458. #pragma mark VectorType
  459. template<> const char * Glue<VectorType>::usr_name() { return "VectorType"; }
  460. template<> const char * Glue<VectorType>::usr_supername() { return "SequentialType"; }
  461. template<> int Glue<VectorType>::usr_tostring(lua_State * L, VectorType * t) { return llvm_print<VectorType>(L, t); }
  462. template<> VectorType * Glue<VectorType>::usr_new(lua_State * L) {
  463. const Type * t = Glue<Type>::checkto(L, 1);
  464. int len = luaL_optinteger(L, 2, 1);
  465. return VectorType::get(t, len);
  466. }
  467. static int vectortype_len(lua_State * L) {
  468. VectorType * u = Glue<VectorType>::checkto(L, 1);
  469. lua_pushinteger(L, u->getNumElements());
  470. return 1;
  471. }
  472. template<> void Glue<VectorType>::usr_mt(lua_State * L, int mt) {
  473. lua_pushcfunction(L, vectortype_len); lua_setfield(L, mt, "__len");
  474. lua_pushcfunction(L, type_eq); lua_setfield(L, mt, "__eq");
  475. }
  476. /*
  477. OpaqueType : DerivedType : Type
  478. */
  479. #pragma mark OpaqueType
  480. template<> const char * Glue<OpaqueType>::usr_name() { return "OpaqueType"; }
  481. template<> const char * Glue<OpaqueType>::usr_supername() { return "Type"; }
  482. template<> int Glue<OpaqueType>::usr_tostring(lua_State * L, OpaqueType * t) { return llvm_print<OpaqueType>(L, t); }
  483. template<> OpaqueType * Glue<OpaqueType>::usr_new(lua_State * L) {
  484. return OpaqueType::get(getGlobalContext());
  485. }
  486. template<> void Glue<OpaqueType>::usr_mt(lua_State * L, int mt) {
  487. lua_pushcfunction(L, type_eq); lua_setfield(L, mt, "__eq");
  488. }
  489. /*
  490. FunctionType : DerivedType : Type
  491. */
  492. #pragma mark FunctionType
  493. template<> const char * Glue<FunctionType>::usr_name() { return "FunctionType"; }
  494. template<> const char * Glue<FunctionType>::usr_supername() { return "Type"; }
  495. template<> int Glue<FunctionType>::usr_tostring(lua_State * L, FunctionType * t) { return llvm_print<FunctionType>(L, t); }
  496. template<> FunctionType * Glue<FunctionType>::usr_new(lua_State * L) {
  497. const Type * ret = Glue<Type>::checkto(L, 1);
  498. std::vector<const Type *> types;
  499. int ntypes = lua_gettop(L) - 1;
  500. for (int i=0; i<ntypes; i++)
  501. types.push_back(Glue<Type>::checkto(L, i+2));
  502. bool isVarArg = false; // this is true for unions, I think??
  503. return FunctionType::get(ret, types, isVarArg);
  504. }
  505. static int functiontype_isvararg(lua_State * L) {
  506. FunctionType * f = Glue<FunctionType>::checkto(L, 1);
  507. lua_pushinteger(L, f->isVarArg());
  508. return 1;
  509. }
  510. static int functiontype_getparamtype(lua_State * L) {
  511. FunctionType * u = Glue<FunctionType>::checkto(L, 1);
  512. unsigned int i = luaL_checkinteger(L, 2);
  513. if (i >= u->getNumParams())
  514. return luaL_error(L, "FunctionType has only %d params (requested %d)", u->getNumParams(), i);
  515. return Glue<Type>::push(L, (Type *)u->getParamType(i));
  516. }
  517. static int functiontype_getreturntype(lua_State * L) {
  518. FunctionType * u = Glue<FunctionType>::checkto(L, 1);
  519. return Glue<Type>::push(L, (Type *)u->getReturnType());
  520. }
  521. static int functiontype_len(lua_State * L) {
  522. FunctionType * u = Glue<FunctionType>::checkto(L, 1);
  523. lua_pushinteger(L, u->getNumParams());
  524. return 1;
  525. }
  526. template<> void Glue<FunctionType>::usr_mt(lua_State * L, int mt) {
  527. lua_pushcfunction(L, type_eq); lua_setfield(L, mt, "__eq");
  528. lua_pushcfunction(L, functiontype_len); lua_setfield(L, mt, "__len");
  529. lua_pushcfunction(L, functiontype_isvararg); lua_setfield(L, mt, "isvararg");
  530. lua_pushcfunction(L, functiontype_getparamtype); lua_setfield(L, mt, "param");
  531. lua_pushcfunction(L, functiontype_getreturntype); lua_setfield(L, mt, "ret");
  532. lua_pushcfunction(L, Glue<Function>::create); lua_setfield(L, mt, "__call");
  533. }
  534. /*
  535. Value
  536. */
  537. #pragma mark Value
  538. template<> const char * Glue<Value>::usr_name() { return "Value"; }
  539. template<> int Glue<Value>::usr_tostring(lua_State * L, Value * u) { return llvm_print<Value>(L, u); }
  540. template<> Value * Glue<Value>::usr_reinterpret(lua_State * L, int idx) {
  541. if (lua_isnoneornil(L, idx))
  542. return 0;
  543. double n;
  544. switch (lua_type(L, idx)) {
  545. case LUA_TBOOLEAN:
  546. // equiv: const bool;
  547. return ConstantInt::get((getGlobalContext()), APInt(1, lua_toboolean(L, idx)));
  548. case LUA_TNUMBER:
  549. n = lua_tonumber(L, idx);
  550. if (fmod(n, 1.0) == 0.)
  551. // equiv: const sint32_t;
  552. return ConstantInt::get((getGlobalContext()), APInt(32, lua_tonumber(L, idx)));
  553. else
  554. // equiv: const double;
  555. return ConstantFP::get(Type::getDoubleTy(getGlobalContext()), lua_tonumber(L, idx));
  556. case LUA_TSTRING:
  557. // equiv: const char * (null terminated)
  558. return ConstantArray::get((getGlobalContext()), std::string(lua_tostring(L, idx)), true); // true == null terminate
  559. case LUA_TUSERDATA: {
  560. // makes sense only if it is a Value:
  561. Value * u = Glue<Value>::checkto(L, idx);
  562. return u;
  563. }
  564. case LUA_TLIGHTUSERDATA: // pointers can't be typeless constants, so exchange for null
  565. case LUA_TNIL:
  566. default: // thread, function & table make no sense for an LLVM Value *
  567. luaL_error(L, "cannot interpret Value from Lua type '%s'\n", lua_typename(L, lua_type(L, idx)));
  568. break;
  569. }
  570. return 0;
  571. }
  572. static int value_type(lua_State * L) {
  573. Value * u = Glue<Value>::checkto(L);
  574. return Glue<Type>::push(L, (Type *)u->getType());
  575. }
  576. static int value_name(lua_State * L) {
  577. Value * u = Glue<Value>::checkto(L);
  578. if (lua_isstring(L, 2)) {
  579. const char * name = lua_tostring(L, 2);
  580. u->setName(name);
  581. lua_pushvalue(L, 1);
  582. return 1;
  583. }
  584. lua_pushstring(L, u->getNameStr().c_str());
  585. return 1;
  586. }
  587. static int value_replace(lua_State * L) {
  588. Value * u = Glue<Value>::checkto(L, 1);
  589. Value * v = Glue<Value>::checkto(L, 2);
  590. u->replaceAllUsesWith(v);
  591. return 0;
  592. }
  593. static int value_uses(lua_State * L) {
  594. Value * u = Glue<Value>::checkto(L, 1);
  595. lua_pushinteger(L, u->getNumUses());
  596. return 1;
  597. }
  598. template<> void Glue<Value>::usr_mt(lua_State * L, int mt) {
  599. lua_pushcfunction(L, value_type); lua_setfield(L, mt, "type");
  600. lua_pushcfunction(L, value_name); lua_setfield(L, mt, "name");
  601. lua_pushcfunction(L, value_replace); lua_setfield(L, mt, "replace");
  602. lua_pushcfunction(L, value_uses); lua_setfield(L, mt, "uses");
  603. }
  604. /*
  605. Argument : Value
  606. */
  607. #pragma mark Argument
  608. template<> const char * Glue<Argument>::usr_name() { return "Argument"; }
  609. template<> const char * Glue<Argument>::usr_supername() { return "Value"; }
  610. template<> int Glue<Argument>::usr_tostring(lua_State * L, Argument * u) { return llvm_print<Argument>(L, u); }
  611. static int argument_parent(lua_State * L) {
  612. Argument * u = Glue<Argument>::checkto(L, 1);
  613. return Glue<Function>::push(L, u->getParent());
  614. }
  615. static int argument_argno(lua_State * L) {
  616. Argument * u = Glue<Argument>::checkto(L, 1);
  617. lua_pushinteger(L, u->getArgNo());
  618. return 1;
  619. }
  620. static int argument_byval(lua_State * L) {
  621. Argument * u = Glue<Argument>::checkto(L, 1);
  622. lua_pushboolean(L, u->hasByValAttr());
  623. return 1;
  624. }
  625. template<> void Glue<Argument>::usr_mt(lua_State * L, int mt) {
  626. lua_pushcfunction(L, argument_parent);
  627. lua_setfield(L, mt, "parent");
  628. lua_pushcfunction(L, argument_argno);
  629. lua_setfield(L, mt, "argno");
  630. lua_pushcfunction(L, argument_byval);
  631. lua_setfield(L, mt, "byval");
  632. }
  633. /*
  634. Instruction : User : Value
  635. */
  636. #pragma mark Instruction
  637. template<> const char * Glue<Instruction>::usr_name() { return "Instruction"; }
  638. template<> const char * Glue<Instruction>::usr_supername() { return "Value"; }
  639. template<> int Glue<Instruction>::usr_tostring(lua_State * L, Instruction * u) { return llvm_print<Instruction>(L, u); }
  640. static int inst_parent(lua_State * L) {
  641. Instruction * u = Glue<Instruction>::checkto(L, 1);
  642. return Glue<BasicBlock>::push(L, u->getParent());
  643. }
  644. static int inst_erase(lua_State * L) {
  645. Instruction * f = Glue<Instruction>::checkto(L, 1);
  646. f->eraseFromParent();
  647. Glue<Instruction>::erase(L, 1);
  648. return 0;
  649. }
  650. static int inst_opcode(lua_State * L) {
  651. Instruction * u = Glue<Instruction>::checkto(L, 1);
  652. lua_pushstring(L, u->getOpcodeName());
  653. return 1;
  654. }
  655. template<> void Glue<Instruction>::usr_mt(lua_State * L, int mt) {
  656. lua_pushcfunction(L, inst_parent); lua_setfield(L, mt, "parent");
  657. lua_pushcfunction(L, inst_erase); lua_setfield(L, mt, "erase");
  658. lua_pushcfunction(L, inst_opcode); lua_setfield(L, mt, "opcode");
  659. }
  660. /*
  661. PHINode : Instruction
  662. */
  663. #pragma mark PHINode
  664. template<> const char * Glue<PHINode>::usr_name() { return "PHINode"; }
  665. template<> const char * Glue<PHINode>::usr_supername() { return "Instruction"; }
  666. template<> int Glue<PHINode>::usr_tostring(lua_State * L, PHINode * u) { return llvm_print<PHINode>(L, u); }
  667. static int phi_addincoming(lua_State * L) {
  668. PHINode * u = Glue<PHINode>::checkto(L, 1);
  669. Value * v = Glue<Value>::checkto(L, 2);
  670. BasicBlock * block = Glue<BasicBlock>::checkto(L, 3);
  671. u->addIncoming(v, block);
  672. return 0;
  673. }
  674. template<> void Glue<PHINode>::usr_mt(lua_State * L, int mt) {
  675. lua_pushcfunction(L, phi_addincoming); lua_setfield(L, mt, "addincoming");
  676. }
  677. /*
  678. BasicBlock : Value
  679. */
  680. #pragma mark BasicBlock
  681. template<> const char * Glue<BasicBlock>::usr_name() { return "BasicBlock"; }
  682. template<> const char * Glue<BasicBlock>::usr_supername() { return "Value"; }
  683. template<> int Glue<BasicBlock>::usr_tostring(lua_State * L, BasicBlock * u) { return llvm_print<BasicBlock>(L, u); }
  684. template<> BasicBlock * Glue<BasicBlock>::usr_new(lua_State * L) {
  685. const char * name = luaL_optstring(L, 1, "noname");
  686. Function * parent = Glue<Function>::to(L, 2);
  687. BasicBlock * insertbefore = Glue<BasicBlock>::to(L, 3);
  688. return BasicBlock::Create((getGlobalContext()), name, parent, insertbefore);
  689. }
  690. static int block_parent(lua_State * L) {
  691. BasicBlock * u = Glue<BasicBlock>::checkto(L, 1);
  692. return Glue<Function>::push(L, u->getParent());
  693. }
  694. static int block_erase(lua_State * L) {
  695. BasicBlock * f = Glue<BasicBlock>::checkto(L, 1);
  696. f->eraseFromParent();
  697. Glue<BasicBlock>::erase(L, 1);
  698. return 0;
  699. }
  700. static int block_terminator(lua_State * L) {
  701. BasicBlock * f = Glue<BasicBlock>::checkto(L, 1);
  702. return Glue<Instruction>::push(L, f->getTerminator());
  703. }
  704. static int block_instruction(lua_State * L) {
  705. BasicBlock * f = Glue<BasicBlock>::checkto(L, 1);
  706. unsigned int i = luaL_checkinteger(L, 2);
  707. if (i >= f->size())
  708. return luaL_error(L, "Function has only %d arguments (requested %d)", f->size(), i);
  709. BasicBlock::iterator it = f->begin();
  710. while (i--) it++;
  711. return Glue<Instruction>::push(L, it);
  712. }
  713. static int block_front(lua_State * L) {
  714. BasicBlock * f = Glue<BasicBlock>::checkto(L, 1);
  715. return Glue<Instruction>::push(L, &f->front());
  716. }
  717. static int block_back(lua_State * L) {
  718. BasicBlock * f = Glue<BasicBlock>::checkto(L, 1);
  719. return Glue<Instruction>::push(L, &f->back());
  720. }
  721. static int block_len(lua_State * L) {
  722. BasicBlock * f = Glue<BasicBlock>::checkto(L, 1);
  723. lua_pushinteger(L, f->size());
  724. return 1;
  725. }
  726. //static int block_setinsertpoint(lua_State * L) {
  727. // BasicBlock * block = Glue<BasicBlock>::checkto(L, 1);
  728. // getLModule(L)->getBuilder()->SetInsertPoint(block);
  729. // return 0;
  730. //}
  731. //static int block_ret(lua_State * L) {
  732. // IRBuilder<> * b = getLModule(L)->getBuilder();
  733. // BasicBlock * block = Glue<BasicBlock>::checkto(L, 1);
  734. // Value * v = Glue<Value>::to(L, 2);
  735. // Function * f = block->getParent();
  736. // if (f) {
  737. // // check types:
  738. // const Type * retType = f->getFunctionType()->getReturnType();
  739. // if (retType->getTypeID() == Type::VoidTyID) {
  740. // if (v)
  741. // return luaL_error(L, "current function returns void");
  742. // return Glue<Instruction>::push(L, b->CreateRetVoid());
  743. // }
  744. // if (retType != v->getType())
  745. // luaL_error(L, "return type mismatch");
  746. // return Glue<Instruction>::push(L, b->CreateRet(v));
  747. // }
  748. // if (v)
  749. // return Glue<Instruction>::push(L, b->CreateRet(v));
  750. // return Glue<Instruction>::push(L, b->CreateRetVoid());
  751. //}
  752. template<> void Glue<BasicBlock>::usr_mt(lua_State * L, int mt) {
  753. lua_pushcfunction(L, block_len); lua_setfield(L, mt, "__len");
  754. lua_pushcfunction(L, block_instruction); lua_setfield(L, mt, "instruction");
  755. lua_pushcfunction(L, block_parent); lua_setfield(L, mt, "parent");
  756. lua_pushcfunction(L, block_front); lua_setfield(L, mt, "front");
  757. lua_pushcfunction(L, block_back); lua_setfield(L, mt, "back");
  758. lua_pushcfunction(L, block_erase); lua_setfield(L, mt, "erase");
  759. lua_pushcfunction(L, block_terminator); lua_setfield(L, mt, "terminator");
  760. // lua_pushcfunction(L, block_setinsertpoint); lua_setfield(L, mt, "setinsertpoint");
  761. // lua_pushcfunction(L, block_ret); lua_setfield(L, mt, "ret");
  762. }
  763. /*
  764. Constant : User : Value
  765. */
  766. #pragma mark Constant
  767. template<> const char * Glue<Constant>::usr_name() { return "Constant"; }
  768. template<> const char * Glue<Constant>::usr_supername() { return "Value"; }
  769. template<> int Glue<Constant>::usr_tostring(lua_State * L, Constant * u) { return llvm_print<Constant>(L, u); }
  770. template<> Constant * Glue<Constant>::usr_reinterpret(lua_State * L, int idx) {
  771. return (Constant *)Glue<Value>::usr_reinterpret(L, idx);
  772. }
  773. // destroyConstant... todo: call this on all constants registered so far, when the module closes
  774. /*
  775. GlobalValue : Constant
  776. */
  777. #pragma mark GlobalValue
  778. template<> const char * Glue<GlobalValue>::usr_name() { return "GlobalValue"; }
  779. template<> const char * Glue<GlobalValue>::usr_supername() { return "Constant"; }
  780. template<> int Glue<GlobalValue>::usr_tostring(lua_State * L, GlobalValue * u) { return llvm_print<GlobalValue>(L, u); }
  781. static int global_linkage(lua_State * L) {
  782. GlobalValue * u = Glue<GlobalValue>::checkto(L, 1);
  783. if (lua_isnumber(L, 2)) {
  784. u->setLinkage((GlobalValue::LinkageTypes)lua_tointeger(L, 2));
  785. }
  786. lua_pushinteger(L, u->getLinkage());
  787. return 1;
  788. }
  789. static int global_isdeclaration(lua_State * L) {
  790. GlobalValue * f = Glue<GlobalValue>::checkto(L, 1);
  791. lua_pushboolean(L, f->isDeclaration());
  792. return 1;
  793. }
  794. static int global_getptr(lua_State * L) {
  795. GlobalValue * u = Glue<GlobalValue>::checkto(L, 1);
  796. if (EE==NULL) luaL_error(L, "No Execution Engine");
  797. void * ptr = EE->getPointerToGlobalIfAvailable(u);
  798. if (ptr) {
  799. lua_pushlightuserdata(L, ptr);
  800. return 1;
  801. }
  802. return 0;
  803. }
  804. template<> void Glue<GlobalValue>::usr_mt(lua_State * L, int mt) {
  805. lua_pushcfunction(L, global_linkage); lua_setfield(L, mt, "linkage");
  806. lua_pushcfunction(L, global_isdeclaration); lua_setfield(L, mt, "isdeclaration");
  807. lua_pushcfunction(L, global_getptr); lua_setfield(L, mt, "getptr");
  808. //lua_pushcfunction(L, ee_addGlobalMapping); lua_setfield(L, mt, "addGlobalMapping");
  809. }
  810. // likely methods: getParent() (returns a Module *)
  811. /*
  812. GlobalVariable : GlobalValue
  813. */
  814. #pragma mark GlobalVariable
  815. template<> const char * Glue<GlobalVariable>::usr_name() { return "GlobalVariable"; }
  816. template<> const char * Glue<GlobalVariable>::usr_supername() { return "GlobalValue"; }
  817. template<> int Glue<GlobalVariable>::usr_tostring(lua_State * L, GlobalVariable * u) { return llvm_print<GlobalVariable>(L, u); }
  818. template<> GlobalVariable * Glue<GlobalVariable>::usr_new(lua_State * L) {
  819. Module * m = Glue<ModuleProvider>::checkto(L, 1)->getModule();
  820. const Type * t = Glue<Type>::checkto(L, 2);
  821. const char * name = luaL_checkstring(L, 3);
  822. bool isConstant = lua_toboolean(L, 4);
  823. Constant * initializer = 0;
  824. if (lua_isuserdata(L, 5)) {
  825. initializer = Glue<Constant>::to(L, 5);
  826. if (initializer->getType() != t) {
  827. luaL_error(L, "initializer must match type");
  828. return 0;
  829. }
  830. }
  831. GlobalValue::LinkageTypes lt = (GlobalValue::LinkageTypes)luaL_optinteger(L, 5, GlobalValue::ExternalLinkage);
  832. return new GlobalVariable((getGlobalContext()), t, isConstant, lt, initializer, name, m);
  833. }
  834. static int globalvar_erase(lua_State * L) {
  835. GlobalVariable * u = Glue<GlobalVariable>::checkto(L, 1);
  836. u->eraseFromParent();
  837. Glue<GlobalVariable>::erase(L, 1);
  838. return 0;
  839. }
  840. static int globalvar_isconstant(lua_State * L) {
  841. GlobalVariable * u = Glue<GlobalVariable>::checkto(L, 1);
  842. lua_pushboolean(L, u->isConstant());
  843. return 1;
  844. }
  845. static int globalvar_initializer(lua_State * L) {
  846. GlobalVariable * u = Glue<GlobalVariable>::checkto(L, 1);
  847. if (u->hasInitializer())
  848. return Glue<Constant>::push(L, u->getInitializer());
  849. return 0;
  850. }
  851. template<> void Glue<GlobalVariable>::usr_mt(lua_State * L, int mt) {
  852. lua_pushcfunction(L, globalvar_erase); lua_setfield(L, mt, "erase");
  853. lua_pushcfunction(L, globalvar_initializer); lua_setfield(L, mt, "initializer");
  854. lua_pushcfunction(L, globalvar_isconstant); lua_setfield(L, mt, "isconstant");
  855. }
  856. /*
  857. Function : GlobalValue
  858. TODO:
  859. When a function is destroyed, we should call
  860. engine->freeMachineCodeForFunction(function);
  861. function->eraseFromParent();
  862. But this isn't safe if another function is using it... so perhaps let the module destructor do it?
  863. */
  864. #pragma mark Function
  865. template<> const char * Glue<Function>::usr_name() { return "Function"; }
  866. template<> const char * Glue<Function>::usr_supername() { return "GlobalValue"; }
  867. template<> int Glue<Function>::usr_tostring(lua_State * L, Function * u) { return llvm_print<Function>(L, u); }
  868. template<> Function * Glue<Function>::usr_new(lua_State * L) {
  869. Module * M = Glue<ModuleProvider>::checkto(L, 1)->getModule();
  870. std::string name = luaL_checkstring(L, 2);
  871. // if argument 3 is nil, then search for a pre-existing function of that name
  872. if (lua_isnoneornil(L, 3))
  873. return M->getFunction(name.data());
  874. // else generate a new function:
  875. const FunctionType * ft = Glue<FunctionType>::checkto(L, 3);
  876. GlobalValue::LinkageTypes lt = (GlobalValue::LinkageTypes)luaL_optinteger(L, 4, GlobalValue::ExternalLinkage);
  877. bool noAutoName = (bool)lua_isboolean(L, 4) && (bool)lua_toboolean(L, 5); // default false
  878. Function * F = Function::Create(ft, lt, name, M);
  879. if (noAutoName && F->getName() != name) {
  880. F->eraseFromParent();
  881. luaL_error(L, "Function %s already exists", name.c_str());
  882. return 0;
  883. }
  884. unsigned int i=0;
  885. Function::arg_iterator AI = F->arg_begin();
  886. for (; i < F->getFunctionType()->getNumParams(); ++AI, ++i) {
  887. char argname[16];
  888. sprintf(argname, "arg%i", i);
  889. AI->setName(argname);
  890. }
  891. return F;
  892. }
  893. static int function_intrinsic(lua_State * L) {
  894. Function * f = Glue<Function>::checkto(L, 1);
  895. lua_pushinteger(L, f->getIntrinsicID());
  896. return 1;
  897. }
  898. static int function_deletebody(lua_State * L) {
  899. Function * f = Glue<Function>::checkto(L, 1);
  900. f->deleteBody();
  901. return 0;
  902. }
  903. static int function_erase(lua_State * L) {
  904. Function * f = Glue<Function>::checkto(L, 1);
  905. f->eraseFromParent();
  906. Glue<Function>::erase(L, 1);
  907. return 0;
  908. }
  909. static int function_callingconv(lua_State * L) {
  910. Function * f = Glue<Function>::checkto(L, 1);
  911. if (lua_isnumber(L, 2)) {
  912. unsigned cc = lua_tonumber(L, 2);
  913. f->setCallingConv(cc);
  914. }
  915. lua_pushinteger(L, f->getCallingConv());
  916. return 1;
  917. }
  918. static int function_argument(lua_State * L) {
  919. Function * f = Glue<Function>::checkto(L, 1);
  920. unsigned int i = luaL_checkinteger(L, 2);
  921. if (i >= f->getFunctionType()->getNumParams())
  922. return luaL_error(L, "Function has only %d arguments (requested %d)", f->getFunctionType()->getNumParams(), i);
  923. Function::arg_iterator it = f->arg_begin();
  924. while (i--) it++;
  925. return Glue<Argument>::push(L, it);
  926. }
  927. static int function_len(lua_State * L) {
  928. Function * f = Glue<Function>::checkto(L, 1);
  929. lua_pushinteger(L, f->size());
  930. return 1;
  931. }
  932. static int function_block(lua_State * L) {
  933. Function * f = Glue<Function>::checkto(L, 1);
  934. unsigned int i = luaL_checkinteger(L, 2);
  935. if (i >= f->size())
  936. return luaL_error(L, "Function has only %d blocks (requested %d)", f->size(), i);
  937. Function::iterator it = f->begin();
  938. while (i--) it++;
  939. return Glue<BasicBlock>::push(L, it);
  940. }
  941. static int function_verify(lua_State * L) {
  942. Function * u = Glue<Function>::to(L, 1);
  943. lua_pushboolean(L, u && (verifyFunction(*u, ReturnStatusAction) == false));
  944. return 1;
  945. }
  946. // trick here is knowing what the right
  947. static int function_getptr(lua_State * L) {
  948. Function * u = Glue<Function>::to(L, 1);
  949. if (EE==NULL) luaL_error(L, "No Execution Engine");
  950. // NOTE: this causes codegeneration of F and any dependents:
  951. void * f = EE->getPointerToFunction(u);
  952. lua_pushlightuserdata(L, f);
  953. return 1;
  954. }
  955. //static int function_optimize(lua_State * L) {
  956. // Function * f = Glue<Function>::checkto(L, 1);
  957. // lua_pushboolean(L, getLModule(L)->optimize(f));
  958. // return 1;
  959. //}
  960. static int function_pushcfunction(lua_State * L) {
  961. Function * u = Glue<Function>::checkto(L, 1);
  962. if (EE==NULL) luaL_error(L, "No Execution Engine");
  963. void * f = EE->getPointerToFunction(u);
  964. if(f) { // && u->getFunctionType() != luaFunctionTy) {
  965. lua_pushcfunction(L, (lua_CFunction)f);
  966. return 1;
  967. }
  968. else {
  969. return 0;
  970. }
  971. }
  972. template<> void Glue<Function>::usr_mt(lua_State * L, int mt) {
  973. lua_pushcfunction(L, function_len); lua_setfield(L, mt, "__len");
  974. lua_pushcfunction(L, function_intrinsic); lua_setfield(L, mt, "intrinsic");
  975. lua_pushcfunction(L, function_deletebody); lua_setfield(L, mt, "deletebody");
  976. lua_pushcfunction(L, function_erase); lua_setfield(L, mt, "erase");
  977. lua_pushcfunction(L, function_callingconv); lua_setfield(L, mt, "callingconv");
  978. lua_pushcfunction(L, function_argument); lua_setfield(L, mt, "argument");
  979. lua_pushcfunction(L, function_block); lua_setfield(L, mt, "block");
  980. lua_pushcfunction(L, function_verify); lua_setfield(L, mt, "verify");
  981. lua_pushcfunction(L, function_getptr); lua_setfield(L, mt, "getptr");
  982. //lua_pushcfunction(L, function_optimize); lua_setfield(L, mt, "optimize");
  983. lua_pushcfunction(L, function_pushcfunction); lua_setfield(L, mt, "pushcfunction");
  984. }
  985. /*
  986. IRBuilder
  987. */
  988. #pragma mark IRBuilder
  989. template<> const char * Glue<IRBuilder<> >::usr_name() { return "IRBuilder"; }
  990. template<> IRBuilder<> * Glue<IRBuilder<> >::usr_new(lua_State * L) {
  991. return new IRBuilder<>(getGlobalContext());
  992. }
  993. static int createPHI(lua_State * L) {
  994. IRBuilder<> * b = Glue<IRBuilder<> >::checkto(L, 1);
  995. Type * ty = Glue<Type>::checkto(L, 2);
  996. const char * name = luaL_optstring(L, 3, "phi");
  997. return Glue<PHINode>::push(L, b->CreatePHI(ty, name));
  998. }
  999. static int createBr(lua_State * L) {
  1000. IRBuilder<> * b = Glue<IRBuilder<> >::checkto(L, 1);
  1001. BasicBlock * branch = Glue<BasicBlock>::checkto(L, 2);
  1002. return Glue<Instruction>::push(L, b->CreateBr(branch));
  1003. }
  1004. static int createCondBr(lua_State * L) {
  1005. IRBuilder<> * b = Glue<IRBuilder<> >::checkto(L, 1);
  1006. Value * condition = Glue<Value>::checkto(L, 2);
  1007. if (condition->getType() != Type::getInt1Ty(getGlobalContext()))
  1008. return luaL_error(L, "condition must be of Int1 type");
  1009. BasicBlock * iftrue = Glue<BasicBlock>::checkto(L, 3);
  1010. BasicBlock * iffalse = Glue<BasicBlock>::checkto(L, 4);
  1011. return Glue<Instruction>::push(L, b->CreateCondBr(condition, iftrue, iffalse));
  1012. }
  1013. static int createRet(lua_State * L) {
  1014. IRBuilder<> * b = Glue<IRBuilder<> >::checkto(L, 1);
  1015. BasicBlock * block = b->GetInsertBlock();
  1016. if (block == 0)
  1017. return luaL_error(L, "not currently building a block");
  1018. Value * v = Glue<Value>::to(L, 2);
  1019. Function * f = block->getParent();
  1020. if (f) {
  1021. // check types:
  1022. const Type * retType = f->getFunctionType()->getReturnType();
  1023. if (retType->getTypeID() == Type::VoidTyID) {
  1024. return Glue<Instruction>::push(L, b->CreateRetVoid());
  1025. }
  1026. if (v == NULL)
  1027. return luaL_error(L, "function requires a return value");
  1028. if (retType->getTypeID() != v->getType()->getTypeID()) {
  1029. //ddebug("%i %i\n", retType->getTypeID(), v->getType()->getTypeID());
  1030. luaL_error(L, "return type mismatch");
  1031. }
  1032. return Glue<Instruction>::push(L, b->CreateRet(v));
  1033. }
  1034. if (v)
  1035. return Glue<Instruction>::push(L, b->CreateRet(v));
  1036. return Glue<Instruction>::push(L, b->CreateRetVoid());
  1037. }
  1038. static int createAdd(lua_State * L) {
  1039. IRBuilder<> * b = Glue<IRBuilder<> >::checkto(L, 1);
  1040. Value * x = Glue<Value>::checkto(L, 2);
  1041. Value * y = Glue<Value>::checkto(L, 3);
  1042. return Glue<Value>::push(L, b->CreateAdd(x, y, "add"));
  1043. }
  1044. static int createSub(lua_State * L) {
  1045. IRBuilder<> * b = Glue<IRBuilder<> >::checkto(L, 1);
  1046. Value * x = Glue<Value>::checkto(L, 2);
  1047. Value * y = Glue<Value>::checkto(L, 3);
  1048. return Glue<Value>::push(L, b->CreateSub(x, y, "sub"));
  1049. }
  1050. static int createMul(lua_State * L) {
  1051. IRBuilder<> * b = Glue<IRBuilder<> >::checkto(L, 1);
  1052. Value * x = Glue<Value>::checkto(L, 2);
  1053. Value * y = Glue<Value>::checkto(L, 3);
  1054. return Glue<Value>::push(L, b->CreateMul(x, y, "mul"));
  1055. }
  1056. static int createDiv(lua_State * L) {
  1057. IRBuilder<> * b = Glue<IRBuilder<> >::checkto(L, 1);
  1058. Value * x = Glue<Value>::checkto(L, 2);
  1059. Value * y = Glue<Value>::checkto(L, 3);
  1060. bool ix = x->getType()->isInteger();
  1061. bool iy = y->getType()->isInteger();
  1062. if (ix && iy) {
  1063. // todo check for unsignedness
  1064. return Glue<Value>::push(L, b->CreateSDiv(x, y, "sdiv"));
  1065. }
  1066. return Glue<Value>::push(L, b->CreateFDiv(x, y, "fdiv"));
  1067. }
  1068. static int createRem(lua_State * L) {
  1069. IRBuilder<> * b = Glue<IRBuilder<> >::checkto(L, 1);
  1070. Value * x = Glue<Value>::checkto(L, 2);
  1071. Value * y = Glue<Value>::checkto(L, 3);
  1072. bool ix = x->getType()->isInteger();
  1073. bool iy = y->getType()->isInteger();
  1074. if (ix && iy) {
  1075. // todo check for unsignedness
  1076. return Glue<Value>::push(L, b->CreateSRem(x, y, "srem"));
  1077. }
  1078. return Glue<Value>::push(L, b->CreateFRem(x, y, "frem"));
  1079. }
  1080. static int createNeg(lua_State * L) {
  1081. IRBuilder<> * b = Glue<IRBuilder<> >::checkto(L, 1);
  1082. return Glue<Value>::push(L, b->CreateNeg(Glue<Value>::checkto(L, 2), "neg"));
  1083. }
  1084. static int createNot(lua_State * L) {
  1085. IRBuilder<> * b = Glue<IRBuilder<> >::checkto(L, 1);
  1086. return Glue<Value>::push(L, b->CreateNot(Glue<Value>::checkto(L, 2), "not"));
  1087. }
  1088. static int createCmpEQ(lua_State * L) {
  1089. IRBuilder<> * b = Glue<IRBuilder<> >::checkto(L, 1);
  1090. Value * x = Glue<Value>::checkto(L, 2);
  1091. Value * y = Glue<Value>::checkto(L, 3);
  1092. // for int: option indicates unsigned; for float: fail if either is nan;
  1093. bool option = lua_isboolean(L, 4) && lua_toboolean(L, 4);
  1094. bool ix = x->getType()->isInteger();
  1095. bool iy = y->getType()->isInteger();
  1096. if (ix && iy) {
  1097. return Glue<Value>::push(L, b->CreateICmpEQ(x, y, "icmp_eq"));
  1098. }
  1099. if (!option)
  1100. return Glue<Value>::push(L, b->CreateFCmpOEQ(x, y, "fcmp_oeq"));
  1101. return Glue<Value>::push(L, b->CreateFCmpUEQ(x, y, "fcmp_ueq"));
  1102. }
  1103. static int createCmpNE(lua_State * L) {
  1104. IRBuilder<> * b = Glue<IRBuilder<> >::checkto(L, 1);
  1105. Value * x = Glue<Value>::checkto(L, 2);
  1106. Value * y = Glue<Value>::checkto(L, 3);
  1107. // for int: option indicates unsigned; for float: fail if either is nan;
  1108. bool option = lua_isboolean(L, 4) && lua_toboolean(L, 4);
  1109. bool ix = x->getType()->isInteger();
  1110. bool iy = y->getType()->isInteger();
  1111. if (ix && iy) {
  1112. return Glue<Value>::push(L, b->CreateICmpNE(x, y, "icmp_ne"));
  1113. }
  1114. if (!option)
  1115. return Glue<Value>::push(L, b->CreateFCmpONE(x, y, "fcmp_one"));
  1116. return Glue<Value>::push(L, b->CreateFCmpUNE(x, y, "fcmp_une"));
  1117. }
  1118. static int createCmpGT(lua_State * L) {
  1119. IRBuilder<> * b = Glue<IRBuilder<> >::checkto(L, 1);
  1120. Value * x = Glue<Value>::checkto(L, 2);
  1121. Value * y = Glue<Value>::checkto(L, 3);
  1122. // for int: option indicates unsigned; for float: fail if either is nan;
  1123. bool option = lua_isboolean(L, 4) && lua_toboolean(L, 4);
  1124. bool ix = x->getType()->isInteger();
  1125. bool iy = y->getType()->isInteger();
  1126. if (ix && iy) {
  1127. if (option)
  1128. return Glue<Value>::push(L, b->CreateICmpUGT(x, y, "icmp_ugt"));
  1129. return Glue<Value>::push(L, b->CreateICmpSGT(x, y, "icmp_sgt"));
  1130. }
  1131. if (!option)
  1132. return Glue<Value>::push(L, b->CreateFCmpOGT(x, y, "fcmp_ogt"));
  1133. return Glue<Value>::push(L, b->CreateFCmpUGT(x, y, "fcmp_ugt"));
  1134. }
  1135. static int createCmpLT(lua_State * L) {
  1136. IRBuilder<> * b = Glue<IRBuilder<> >::checkto(L, 1);
  1137. Value * x = Glue<Value>::checkto(L, 2);
  1138. Value * y = Glue<Value>::checkto(L, 3);
  1139. // for int: option indicates unsigned; for float: fail if either is nan;
  1140. bool option = lua_isboolean(L, 4) && lua_toboolean(L, 4);
  1141. bool ix = x->getType()->isInteger();
  1142. bool iy = y->getType()->isInteger();
  1143. if (ix && iy) {
  1144. if (option)
  1145. return Glue<Value>::push(L, b->CreateICmpULT(x, y, "icmp_ult"));
  1146. return Glue<Value>::push(L, b->CreateICmpSLT(x, y, "icmp_slt"));
  1147. }
  1148. if (!option)
  1149. return Glue<Value>::push(L, b->CreateFCmpOLT(x, y, "fcmp_olt"));
  1150. return Glue<Value>::push(L, b->CreateFCmpULT(x, y, "fcmp_ult"));
  1151. }
  1152. static int createCmpGE(lua_State * L) {
  1153. IRBuilder<> * b = Glue<IRBuilder<> >::checkto(L, 1);
  1154. Value * x = Glue<Value>::checkto(L, 2);
  1155. Value * y = Glue<Value>::checkto(L, 3);
  1156. // for int: option indicates unsigned; for float: fail if either is nan;
  1157. bool option = lua_isboolean(L, 4) && lua_toboolean(L, 4);
  1158. bool ix = x->getType()->isInteger();
  1159. bool iy = y->getType()->isInteger();
  1160. if (ix && iy) {
  1161. if (option)
  1162. return Glue<Value>::push(L, b->CreateICmpUGE(x, y, "icmp_uge"));
  1163. return Glue<Value>::push(L, b->CreateICmpSGE(x, y, "icmp_sge"));
  1164. }
  1165. if (!option)
  1166. return Glue<Value>::push(L, b->CreateFCmpOGE(x, y, "fcmp_oge"));
  1167. return Glue<Value>::push(L, b->CreateFCmpUGE(x, y, "fcmp_uge"));
  1168. }
  1169. static int createCmpLE(lua_State * L) {
  1170. IRBuilder<> * b = Glue<IRBuilder<> >::checkto(L, 1);
  1171. Value * x = Glue<Value>::checkto(L, 2);
  1172. Value * y = Glue<Value>::checkto(L, 3);
  1173. // for int: option indicates unsigned; for float: fail if either is nan;
  1174. bool option = lua_isboolean(L, 4) && lua_toboolean(L, 4);
  1175. bool ix = x->getType()->isInteger();
  1176. bool iy = y->getType()->isInteger();
  1177. if (ix && iy) {
  1178. if (option)
  1179. return Glue<Value>::push(L, b->CreateICmpULE(x, y, "icmp_ule"));
  1180. return Glue<Value>::push(L, b->CreateICmpSLE(x, y, "icmp_sle"));
  1181. }
  1182. if (!option)
  1183. return Glue<Value>::push(L, b->CreateFCmpOLE(x, y, "fcmp_ole"));
  1184. return Glue<Value>::push(L, b->CreateFCmpULE(x, y, "fcmp_ule"));
  1185. }
  1186. static int createFToI(lua_State * L) {
  1187. IRBuilder<> * b = Glue<IRBuilder<> >::checkto(L, 1);
  1188. Value * x = Glue<Value>::checkto(L, 2);
  1189. // option indicates unsigned
  1190. bool option = lua_isboolean(L, 3) && lua_toboolean(L, 3);
  1191. if (!x->getType()->isFloatingPoint())
  1192. luaL_error(L, "attempt to cast from non-floating point type");
  1193. if (!option)
  1194. return Glue<Value>::push(L, b->CreateFPToSI(x, Type::getInt32Ty(getGlobalContext()), "fptosi"));
  1195. return Glue<Value>::push(L, b->CreateFPToUI(x, Type::getInt32Ty(getGlobalContext()), "fptoui"));
  1196. }
  1197. static int createIToF(lua_State * L) {
  1198. IRBuilder<> * b = Glue<IRBuilder<> >::checkto(L, 1);
  1199. Value * x = Glue<Value>::checkto(L, 2);
  1200. // option indicates unsigned
  1201. bool option = lua_isboolean(L, 3) && lua_toboolean(L, 3);
  1202. if (!x->getType()->isInteger())
  1203. luaL_error(L, "attempt to cast from non-integer type");
  1204. if (!option)
  1205. return Glue<Value>::push(L, b->CreateSIToFP(x, Type::getDoubleTy(getGlobalContext()), "sitofp"));
  1206. return Glue<Value>::push(L, b->CreateUIToFP(x, Type::getDoubleTy(getGlobalContext()), "uitofp"));
  1207. }
  1208. static int createCall(lua_State * L) {
  1209. IRBuilder<> * b = Glue<IRBuilder<> >::checkto(L, 1);
  1210. Module * m = Glue<ModuleProvider>::checkto(L, 2)->getModule();
  1211. // get args:
  1212. std::vector<Value *> args;
  1213. for (int i=4; i<=lua_gettop(L); i++) {
  1214. args.push_back(Glue<Value>::checkto(L, i));
  1215. }
  1216. // function may be by Function or by name:
  1217. if (lua_isstring(L, 3)) {
  1218. const char * fname = lua_tostring(L, 3);
  1219. Function * f = m->getFunction(fname);
  1220. if (f == 0)
  1221. return luaL_error(L, "function %s not found", lua_tostring(L, 3));
  1222. if (args.size() < f->getFunctionType()->getNumParams())
  1223. return luaL_error(L, "insufficient arguments");
  1224. // ok it's a function. what return type?
  1225. if (f->getReturnType() == Type::getVoidTy(getGlobalContext())) {
  1226. // functions returning void shouldn't be named:
  1227. b->CreateCall(f, args.begin(), args.end());
  1228. return 0;
  1229. }
  1230. return Glue<Value>::push(L, b->CreateCall(f, args.begin(), args.end(), f->getNameStr().c_str()));
  1231. }
  1232. Function * f = Glue<Function>::checkto(L, 3);
  1233. FunctionType * ft = (FunctionType *)f->getFunctionType();
  1234. if (args.size() < ft->getNumParams())
  1235. return luaL_error(L, "insufficient arguments");
  1236. if (f->getReturnType() == Type::getVoidTy(getGlobalContext())) {
  1237. // functions returning void shouldn't be named:
  1238. b->CreateCall(f, args.begin(), a

Large files files are truncated, but you can click here to view the full file