forked from terralang/terra
-
Notifications
You must be signed in to change notification settings - Fork 2
/
terra.cpp
611 lines (542 loc) · 18.7 KB
/
terra.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
/* See Copyright Notice in ../LICENSE.txt */
#include "terra.h"
#include "terrastate.h"
#include "llex.h"
#include "lstring.h"
#include "lzio.h"
#include "lparser.h"
#include "tcompiler.h"
#include "tkind.h"
#include "tcwrapper.h"
#include "tcuda.h"
#include "tdebug.h"
#include <stdio.h>
#include <stdarg.h>
#include <assert.h>
#ifndef _WIN32
#include <dlfcn.h>
#include <libgen.h>
#include <unistd.h>
#else
#include "twindows.h"
#include <string>
#include <Shlwapi.h>
#include <comdef.h>
#include "MSVCSetupAPI.h"
_COM_SMARTPTR_TYPEDEF(ISetupConfiguration, __uuidof(ISetupConfiguration));
_COM_SMARTPTR_TYPEDEF(ISetupConfiguration2, __uuidof(ISetupConfiguration2));
_COM_SMARTPTR_TYPEDEF(ISetupHelper, __uuidof(ISetupHelper));
_COM_SMARTPTR_TYPEDEF(IEnumSetupInstances, __uuidof(IEnumSetupInstances));
_COM_SMARTPTR_TYPEDEF(ISetupInstance, __uuidof(ISetupInstance));
_COM_SMARTPTR_TYPEDEF(ISetupInstance2, __uuidof(ISetupInstance2));
#endif
static char *vstringf(const char *fmt, va_list ap) {
int N = 128;
char *buf = (char *)malloc(N);
while (1) {
va_list cp;
#if _MSC_VER < 1900 && defined(_WIN32)
cp = ap;
#else
va_copy(cp, ap);
#endif
int n = vsnprintf(buf, N, fmt, cp);
if (n > -1 && n < N) {
return buf;
}
if (n > -1)
N = n + 1;
else
N *= 2;
free(buf);
buf = (char *)malloc(N);
}
}
void terra_vpusherror(terra_State *T, const char *fmt, va_list ap) {
char *buf = vstringf(fmt, ap);
lua_pushstring(T->L, buf);
free(buf);
}
void terra_pusherror(terra_State *T, const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
terra_vpusherror(T, fmt, ap);
va_end(ap);
}
void terra_reporterror(terra_State *T, const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
terra_vpusherror(T, fmt, ap);
va_end(ap);
lua_error(T->L);
}
terra_State *terra_getstate(lua_State *L, int upvalue) {
terra_State *T = const_cast<terra_State *>(
(const terra_State *)lua_topointer(L, lua_upvalueindex(upvalue)));
assert(T);
T->L = L;
return T;
}
static terra_State *getterra(lua_State *L) {
lua_getfield(L, LUA_GLOBALSINDEX, "terra");
lua_getfield(L, -1, "__terrastate");
terra_State *T = (terra_State *)lua_touserdata(L, -1);
T->L = L;
lua_pop(L, 2);
return T;
}
// implementations of lua functions load, loadfile, loadstring
static const char *reader_luaload(lua_State *L, void *ud, size_t *size) {
size_t fnvalue = (size_t)ud;
lua_pushvalue(L, fnvalue);
lua_call(L, 0, 1);
if (lua_isnil(L, -1)) {
lua_pop(L, 1);
return NULL;
}
const char *str = lua_tolstring(L, -1, size);
lua_pop(L, 1);
return str;
}
int terra_luaload(lua_State *L) {
const char *chunkname = "=(load)";
size_t fnvalue;
if (lua_gettop(L) == 2) {
chunkname = luaL_checkstring(L, -1);
fnvalue = lua_gettop(L) - 1;
} else {
fnvalue = lua_gettop(L);
}
if (terra_load(L, reader_luaload, (void *)fnvalue, chunkname)) {
lua_pushnil(L);
lua_pushvalue(L, -2);
lua_remove(L, -3);
return 2;
}
return 1;
}
int terra_lualoadfile(lua_State *L) {
const char *file = NULL;
if (lua_gettop(L) > 0) file = luaL_checkstring(L, -1);
if (terra_loadfile(L, file)) {
lua_pushnil(L);
lua_pushvalue(L, -2);
lua_remove(L, -3);
return 2;
}
return 1;
}
int terra_lualoadstring(lua_State *L) {
const char *string = luaL_checkstring(L, -1);
if (terra_loadstring(L, string)) {
lua_pushnil(L);
lua_pushvalue(L, -2);
lua_remove(L, -3);
return 2;
}
return 1;
}
// defines bytecodes for included lua source
#include "terralib.h"
#include "strict.h"
#include "asdl.h"
#include "terralist.h"
int terra_loadandrunbytecodes(lua_State *L, const unsigned char *bytecodes, size_t size,
const char *name) {
return luaL_loadbuffer(L, (const char *)bytecodes, size, name) ||
lua_pcall(L, 0, LUA_MULTRET, 0);
}
#define abs_index(L, i) \
((i) > 0 || (i) <= LUA_REGISTRYINDEX ? (i) : lua_gettop(L) + (i) + 1)
void terra_ongc(lua_State *L, int idx, lua_CFunction gcfn) {
idx = abs_index(L, idx);
lua_newtable(L);
lua_pushcfunction(L, gcfn);
lua_setfield(L, -2, "__gc");
lua_setmetatable(L, idx);
}
static int terra_free(lua_State *L);
#ifndef _WIN32
static bool pushterrahome(lua_State *L) {
Dl_info info;
if (dladdr((void *)terra_init, &info) != 0) {
#ifdef __linux__
Dl_info infomain;
void *lmain = dlsym(NULL, "main");
if (dladdr(lmain, &infomain) != 0) {
if (infomain.dli_fbase == info.dli_fbase) {
// statically compiled on linux, we need to find the executable directly.
char *exe_path = realpath("/proc/self/exe", NULL);
if (exe_path) {
lua_pushstring(L, dirname(dirname(exe_path)));
free(exe_path);
return true;
}
}
}
#endif
if (info.dli_fname) {
char *full = realpath(info.dli_fname, NULL);
lua_pushstring(L, dirname(dirname(full))); // TODO: dirname not reentrant
free(full);
return true;
}
}
return false;
}
#else
static bool pushterrahome(lua_State *L) {
// based on approach that clang uses as well
MEMORY_BASIC_INFORMATION mbi;
char path[MAX_PATH];
VirtualQuery((void *)terra_init, &mbi, sizeof(mbi));
GetModuleFileNameA((HINSTANCE)mbi.AllocationBase, path, MAX_PATH);
PathRemoveFileSpecA(path);
PathRemoveFileSpecA(path);
lua_pushstring(L, path);
return true;
}
// On windows, gets the value of a given registry key in HKEY_LOCAL_MACHINE, or returns
// nil otherwise.
static int query_reg_value(lua_State *L) {
if (lua_gettop(L) != 2)
lua_pushnil(L);
else {
auto key = luaL_checkstring(L, 1);
auto value = luaL_checkstring(L, 2);
HKEY pkey;
if (!key || !value ||
RegOpenKeyExA(HKEY_LOCAL_MACHINE, key, 0,
KEY_QUERY_VALUE | KEY_WOW64_32KEY | KEY_ENUMERATE_SUB_KEYS,
&pkey) != S_OK)
lua_pushnil(L);
else {
DWORD type = 0;
DWORD sz = 0;
LSTATUS r = RegQueryValueExA(pkey, value, 0, &type, 0, &sz);
if (r != S_OK)
lua_pushnil(L);
else {
BYTE *data = (BYTE *)malloc(sz + 1);
r = RegQueryValueExA(pkey, value, 0, &type, data, &sz);
data[sz] = 0;
if (!data || r != S_OK)
lua_pushnil(L);
else {
switch (type) {
case REG_BINARY:
lua_pushlstring(L, (const char *)data, sz);
break;
case REG_DWORD:
lua_pushinteger(L, *(int32_t *)data);
break;
case REG_QWORD:
lua_pushinteger(
L, *(int64_t *)data); // This doesn't really work but
// we attempt it anyway
break;
case REG_EXPAND_SZ: {
sz = ExpandEnvironmentStringsA((const char *)data, 0, 0);
char *buf = (char *)malloc(sz);
ExpandEnvironmentStringsA((const char *)data, buf, sz);
lua_pushstring(L, (const char *)buf);
free(buf);
} break;
case REG_MULTI_SZ:
case REG_SZ:
lua_pushstring(L, (const char *)data);
break;
}
}
if (data) free(data);
}
RegCloseKey(pkey);
}
}
return 1;
}
struct InitializeCOMRAII {
InitializeCOMRAII() { ::CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED); }
~InitializeCOMRAII() { ::CoUninitialize(); }
};
// Based on clang's MSVC.cpp file
static bool findVCToolChainViaSetupConfig(lua_State *L) {
InitializeCOMRAII com;
HRESULT HR;
ISetupConfigurationPtr Query;
HR = Query.CreateInstance(__uuidof(SetupConfiguration));
if (FAILED(HR)) return false;
IEnumSetupInstancesPtr EnumInstances;
HR = ISetupConfiguration2Ptr(Query)->EnumAllInstances(&EnumInstances);
if (FAILED(HR)) return false;
ISetupInstancePtr Instance;
HR = EnumInstances->Next(1, &Instance, nullptr);
if (HR != S_OK) return false;
ISetupInstancePtr NewestInstance;
uint64_t NewestVersionNum = 0;
do {
bstr_t VersionString;
uint64_t VersionNum;
HR = Instance->GetInstallationVersion(VersionString.GetAddress());
if (FAILED(HR)) continue;
HR = ISetupHelperPtr(Query)->ParseVersion(VersionString, &VersionNum);
if (FAILED(HR)) continue;
if (!NewestVersionNum || (VersionNum > NewestVersionNum)) {
NewestInstance = Instance;
NewestVersionNum = VersionNum;
}
} while ((HR = EnumInstances->Next(1, &Instance, nullptr)) == S_OK);
if (!NewestInstance) return false;
bstr_t VCPathWide;
HR = NewestInstance->ResolvePath(L"VC", VCPathWide.GetAddress());
if (FAILED(HR)) return false;
std::wstring ToolsVersionFilePath(VCPathWide);
ToolsVersionFilePath += L"\\Auxiliary\\Build\\Microsoft.VCToolsVersion.default.txt";
FILE *f = nullptr;
auto open_result = _wfopen_s(&f, ToolsVersionFilePath.c_str(), L"rt");
if (open_result != 0 || !f) return false;
fseek(f, 0, SEEK_END);
size_t tools_file_size = ftell(f);
fseek(f, 0, SEEK_SET);
std::string version;
version.resize(tools_file_size);
auto retval = fgets((char *)version.data(), version.size(), f);
fclose(f);
if (!retval) return false;
while (!version.back()) // strip trailing null terminators so whitespace removal
// works
version.pop_back();
version.erase(version.find_last_not_of(" \n\r\t\v") + 1);
std::string ToolchainPath;
ToolchainPath.resize(WideCharToMultiByte(CP_UTF8, 0, VCPathWide, VCPathWide.length(),
0, 0, NULL, NULL));
ToolchainPath.resize(WideCharToMultiByte(CP_UTF8, 0, VCPathWide, VCPathWide.length(),
(char *)ToolchainPath.data(),
ToolchainPath.capacity(), NULL, NULL));
ToolchainPath += "\\Tools\\MSVC\\";
ToolchainPath += version;
lua_pushstring(L, ToolchainPath.c_str());
return true;
}
static int find_visual_studio_toolchain(lua_State *L) {
if (!findVCToolChainViaSetupConfig(L)) lua_pushnil(L);
return 1;
}
static int list_subdirectories(lua_State *L) {
if (lua_gettop(L) != 1)
lua_pushnil(L);
else {
std::string path = luaL_checkstring(L, 1);
if (path.back() != '\\') path += '\\';
path += '*';
WIN32_FIND_DATAA find_data;
auto handle = FindFirstFileA(path.c_str(), &find_data);
if (handle == INVALID_HANDLE_VALUE)
lua_pushnil(L);
else {
lua_newtable(L);
int i = 0;
BOOL success = TRUE;
while (success) {
if ((find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&
(find_data.cFileName[0] != '.')) {
lua_pushnumber(L, ++i); // Make sure we start at 1
lua_pushstring(L, find_data.cFileName);
lua_settable(L, -3);
}
success = FindNextFileA(handle, &find_data);
}
FindClose(handle);
}
}
return 1;
}
#endif
static void setterrahome(lua_State *L) {
lua_getfield(L, LUA_GLOBALSINDEX, "terra");
if (pushterrahome(L)) lua_setfield(L, -2, "terrahome");
lua_pop(L, 1);
}
int terra_init(lua_State *L) {
terra_Options options;
memset(&options, 0, sizeof(terra_Options));
return terra_initwithoptions(L, &options);
}
int terra_initwithoptions(lua_State *L, terra_Options *options) {
terra_State *T = (terra_State *)lua_newuserdata(L, sizeof(terra_State));
terra_ongc(L, -1, terra_free);
assert(T);
memset(T, 0, sizeof(terra_State)); // some of lua stuff expects pointers to be null
// on entry
T->options = *options;
T->numlivefunctions = 1;
T->L = L;
assert(T->L);
lua_newtable(T->L);
lua_insert(L, -2);
lua_setfield(L, -2, "__terrastate"); // reference to our T object, so that we can
// load it from the lua state on other API calls
lua_setfield(T->L, LUA_GLOBALSINDEX, "terra"); // create global terra object
terra_kindsinit(T); // initialize lua mapping from T_Kind to/from string
setterrahome(T->L); // find the location of support files such as the clang resource
// directory
int err = terra_compilerinit(T);
if (err) {
return err;
}
#ifdef _WIN32
// These functions are required by terralib.lua
lua_getfield(T->L, LUA_GLOBALSINDEX, "terra");
lua_pushcfunction(T->L, query_reg_value);
lua_setfield(T->L, -2, "queryregvalue");
lua_pushcfunction(T->L, find_visual_studio_toolchain);
lua_setfield(T->L, -2, "findvisualstudiotoolchain");
lua_pushcfunction(T->L, list_subdirectories);
lua_setfield(T->L, -2, "listsubdirectories");
lua_pop(T->L, 1); //'terra' global
#endif
err = terra_loadandrunbytecodes(T->L, (const unsigned char *)luaJIT_BC_strict,
luaJIT_BC_strict_SIZE, "strict.lua") ||
terra_loadandrunbytecodes(T->L, (const unsigned char *)luaJIT_BC_terralist,
luaJIT_BC_terralist_SIZE, "terralist.lua") ||
terra_loadandrunbytecodes(T->L, (const unsigned char *)luaJIT_BC_asdl,
luaJIT_BC_asdl_SIZE, "asdl.lua")
#ifndef TERRA_EXTERNAL_TERRALIB
|| terra_loadandrunbytecodes(T->L, (const unsigned char *)luaJIT_BC_terralib,
luaJIT_BC_terralib_SIZE, "terralib.lua");
#else
// make it possible to quickly iterate in terralib.lua when developing
|| luaL_loadfile(T->L, TERRA_EXTERNAL_TERRALIB) ||
lua_pcall(L, 0, LUA_MULTRET, 0);
#endif
if (err) {
return err;
}
terra_cwrapperinit(T);
lua_getfield(T->L, LUA_GLOBALSINDEX, "terra");
lua_pushcfunction(T->L, terra_luaload);
lua_setfield(T->L, -2, "load");
lua_pushcfunction(T->L, terra_lualoadstring);
lua_setfield(T->L, -2, "loadstring");
lua_pushcfunction(T->L, terra_lualoadfile);
lua_setfield(T->L, -2, "loadfile");
lua_pushstring(T->L, TERRA_VERSION_STRING);
lua_setfield(T->L, -2, "version");
lua_pushinteger(T->L, LLVM_VERSION);
lua_setfield(L, -2, "llvm_version");
lua_newtable(T->L);
lua_setfield(T->L, -2, "_trees"); // to hold parser generated trees
lua_pushinteger(L, T->options.verbose);
lua_setfield(L, -2, "isverbose");
lua_pushinteger(L, T->options.debug);
lua_setfield(L, -2, "isdebug");
terra_registerinternalizedfiles(L, -1);
lua_pop(T->L, 1); //'terra' global
luaX_init(T);
terra_debuginit(T);
err = terra_cudainit(T); /* if cuda is not enabled, this does nothing */
if (err) {
return err;
}
return 0;
}
// Called when the lua state object is free'd during lua_close
static int terra_free(lua_State *L) {
terra_State *T = (terra_State *)lua_touserdata(L, -1);
assert(T);
terra_cudafree(T);
terra_compilerfree(T->C);
for (TerraTarget *TT : T->targets) {
freetarget(TT);
}
return 0;
}
struct FileInfo {
FILE *file;
char buf[512];
};
int terra_load(lua_State *L, lua_Reader reader, void *data, const char *chunkname) {
int st = lua_gettop(L);
terra_State *T = getterra(L);
Zio zio;
luaZ_init(T, &zio, reader, data);
int r = luaY_parser(T, &zio, chunkname, zgetc(&zio));
assert(lua_gettop(L) == st + 1);
return r;
}
// these helper functions are from the LuaJIT implementation for loadfile and loadstring:
#define TERRA_BUFFERSIZE 512
typedef struct FileReaderCtx {
FILE *fp;
char buf[TERRA_BUFFERSIZE];
} FileReaderCtx;
static const char *reader_file(lua_State *L, void *ud, size_t *size) {
FileReaderCtx *ctx = (FileReaderCtx *)ud;
if (feof(ctx->fp)) return NULL;
*size = fread(ctx->buf, 1, sizeof(ctx->buf), ctx->fp);
return *size > 0 ? ctx->buf : NULL;
}
typedef struct StringReaderCtx {
const char *str;
size_t size;
} StringReaderCtx;
static const char *reader_string(lua_State *L, void *ud, size_t *size) {
StringReaderCtx *ctx = (StringReaderCtx *)ud;
if (ctx->size == 0) return NULL;
*size = ctx->size;
ctx->size = 0;
return ctx->str;
}
// end helper functions
int terra_loadfile(lua_State *L, const char *file) {
FileReaderCtx ctx;
ctx.fp = file ? fopen(file, "r") : stdin;
if (!ctx.fp) {
terra_State *T = getterra(L);
terra_pusherror(T, "failed to open file '%s'", file);
return LUA_ERRFILE;
}
/*peek to see if we have a POSIX comment '#', which we repect on the first like for #!
*/
int c = fgetc(ctx.fp);
ungetc(c, ctx.fp);
if (c == '#') { /* skip the POSIX comment */
do {
c = fgetc(ctx.fp);
} while (c != '\n' && c != EOF);
if (c == '\n') ungetc(c, ctx.fp); /* keep line count accurate */
}
if (file) {
size_t name_size = strlen(file) + 2;
char *name = (char *)malloc(name_size);
snprintf(name, name_size, "@%s", file);
int r = terra_load(L, reader_file, &ctx, name);
free(name);
fclose(ctx.fp);
return r;
} else {
return terra_load(L, reader_file, &ctx, "@=stdin");
}
}
int terra_loadbuffer(lua_State *L, const char *buf, size_t size, const char *name) {
StringReaderCtx ctx;
ctx.str = buf;
ctx.size = size;
return terra_load(L, reader_string, &ctx, name);
}
int terra_loadstring(lua_State *L, const char *s) {
return terra_loadbuffer(L, s, strlen(s), "<string>");
}
namespace llvm {
void llvm_shutdown();
}
void terra_llvmshutdown() { llvm::llvm_shutdown(); }
// for require
extern "C" int luaopen_terra(lua_State *L) {
terra_Options options;
memset(&options, 0, sizeof(terra_Options));
if (!lua_isnil(L, 1)) options.verbose = lua_tonumber(L, 1);
if (!lua_isnil(L, 2)) options.debug = lua_tonumber(L, 2);
if (terra_initwithoptions(L, &options)) lua_error(L);
return 0;
}