@@ -1248,26 +1248,13 @@ static void emitInjTags(KOREDefinition *def, llvm::Module *mod) {
1248
1248
}
1249
1249
}
1250
1250
1251
- static void emitSortTable (KOREDefinition *definition, llvm::Module *module) {
1252
- llvm::LLVMContext &Ctx = module->getContext ();
1253
- auto &syms = definition->getSymbols ();
1254
- auto ty = llvm::PointerType::getUnqual (llvm::Type::getInt8PtrTy (Ctx));
1255
- auto dity = getPointerDebugType (getCharPtrDebugType (), " char *" );
1256
- auto tableType = llvm::ArrayType::get (ty, syms.size ());
1257
- auto table = module->getOrInsertGlobal (" sort_table" , tableType);
1258
- llvm::GlobalVariable *globalVar = llvm::dyn_cast<llvm::GlobalVariable>(table);
1259
- initDebugGlobal (
1260
- " sort_table" ,
1261
- getArrayDebugType (
1262
- dity, syms.size (), llvm::DataLayout (module).getABITypeAlign (ty)),
1263
- globalVar);
1264
- std::vector<llvm::Constant *> values;
1265
- for (auto iter = syms.begin (); iter != syms.end (); ++iter) {
1266
- auto entry = *iter;
1267
- auto symbol = entry.second ;
1251
+ static void emitSortTable (KOREDefinition *def, llvm::Module *mod) {
1252
+ auto getter = [](KOREDefinition *definition, llvm::Module *module,
1253
+ KORESymbol *symbol) -> llvm::Constant * {
1254
+ auto &ctx = module->getContext ();
1268
1255
1269
1256
auto subtableType = llvm::ArrayType::get (
1270
- llvm::Type::getInt8PtrTy (Ctx ), symbol->getArguments ().size ());
1257
+ llvm::Type::getInt8PtrTy (ctx ), symbol->getArguments ().size ());
1271
1258
auto subtable = module->getOrInsertGlobal (
1272
1259
fmt::format (" sorts_{}" , ast_to_string (*symbol)), subtableType);
1273
1260
llvm::GlobalVariable *subtableVar
@@ -1277,30 +1264,35 @@ static void emitSortTable(KOREDefinition *definition, llvm::Module *module) {
1277
1264
getArrayDebugType (
1278
1265
getCharPtrDebugType (), symbol->getArguments ().size (),
1279
1266
llvm::DataLayout (module).getABITypeAlign (
1280
- llvm::Type::getInt8PtrTy (Ctx ))),
1267
+ llvm::Type::getInt8PtrTy (ctx ))),
1281
1268
subtableVar);
1282
1269
llvm::Constant *zero
1283
- = llvm::ConstantInt::get (llvm::Type::getInt64Ty (Ctx ), 0 );
1270
+ = llvm::ConstantInt::get (llvm::Type::getInt64Ty (ctx ), 0 );
1284
1271
auto indices = std::vector<llvm::Constant *>{zero, zero};
1285
- values.push_back (llvm::ConstantExpr::getInBoundsGetElementPtr (
1286
- subtableType, subtableVar, indices));
1287
1272
1288
1273
std::vector<llvm::Constant *> subvalues;
1289
1274
for (size_t i = 0 ; i < symbol->getArguments ().size (); ++i) {
1290
1275
auto arg_str = ast_to_string (*symbol->getArguments ()[i]);
1291
1276
auto strType = llvm::ArrayType::get (
1292
- llvm::Type::getInt8Ty (Ctx ), arg_str.size () + 1 );
1277
+ llvm::Type::getInt8Ty (ctx ), arg_str.size () + 1 );
1293
1278
auto sortName = module->getOrInsertGlobal (
1294
1279
fmt::format (" sort_name_{}" , arg_str), strType);
1295
1280
subvalues.push_back (llvm::ConstantExpr::getInBoundsGetElementPtr (
1296
1281
strType, sortName, indices));
1297
1282
}
1298
1283
subtableVar->setInitializer (
1299
1284
llvm::ConstantArray::get (subtableType, subvalues));
1300
- }
1301
- if (!globalVar->hasInitializer ()) {
1302
- globalVar->setInitializer (llvm::ConstantArray::get (tableType, values));
1303
- }
1285
+
1286
+ return llvm::ConstantExpr::getInBoundsGetElementPtr (
1287
+ subtableType, subtableVar, indices);
1288
+ };
1289
+
1290
+ auto i8_ptr_ty = llvm::Type::getInt8PtrTy (mod->getContext ());
1291
+ auto entry_ty = llvm::PointerType::getUnqual (i8_ptr_ty);
1292
+ auto debug_ty = getPointerDebugType (getCharPtrDebugType (), " char **" );
1293
+
1294
+ emitDataTableForSymbol (
1295
+ " getArgumentSortsForTag" , entry_ty, debug_ty, def, mod, getter);
1304
1296
}
1305
1297
1306
1298
/*
@@ -1311,23 +1303,12 @@ static void emitSortTable(KOREDefinition *definition, llvm::Module *module) {
1311
1303
*
1312
1304
* Each value in the table is a pointer to a global variable containing the
1313
1305
* relevant sort name as a null-terminated string.
1314
- *
1315
- * The function `getReturnSortForTag` abstracts accesses to the data in this
1316
- * table.
1317
1306
*/
1318
- static void
1319
- emitReturnSortTable (KOREDefinition *definition, llvm::Module *module) {
1320
- auto &ctx = module->getContext ();
1307
+ static void emitReturnSortTable (KOREDefinition *def, llvm::Module *mod) {
1308
+ auto getter = [](KOREDefinition *definition, llvm::Module *module,
1309
+ KORESymbol *symbol) -> llvm::Constant * {
1310
+ auto &ctx = module->getContext ();
1321
1311
1322
- auto const &syms = definition->getSymbols ();
1323
-
1324
- auto element_type = llvm::Type::getInt8PtrTy (ctx);
1325
- auto table_type = llvm::ArrayType::get (element_type, syms.size ());
1326
-
1327
- auto table = module->getOrInsertGlobal (" return_sort_table" , table_type);
1328
- auto values = std::vector<llvm::Constant *>{};
1329
-
1330
- for (auto [tag, symbol] : syms) {
1331
1312
auto sort = symbol->getSort ();
1332
1313
auto sort_str = ast_to_string (*sort);
1333
1314
@@ -1340,16 +1321,13 @@ emitReturnSortTable(KOREDefinition *definition, llvm::Module *module) {
1340
1321
auto i64_type = llvm::Type::getInt64Ty (ctx);
1341
1322
auto zero = llvm::ConstantInt::get (i64_type, 0 );
1342
1323
1343
- auto pointer = llvm::ConstantExpr::getInBoundsGetElementPtr (
1324
+ return llvm::ConstantExpr::getInBoundsGetElementPtr (
1344
1325
str_type, sort_name, std::vector<llvm::Constant *>{zero});
1326
+ };
1345
1327
1346
- values.push_back (pointer);
1347
- }
1348
-
1349
- auto global = llvm::dyn_cast<llvm::GlobalVariable>(table);
1350
- if (!global->hasInitializer ()) {
1351
- global->setInitializer (llvm::ConstantArray::get (table_type, values));
1352
- }
1328
+ emitDataTableForSymbol (
1329
+ " getReturnSortForTag" , llvm::Type::getInt8PtrTy (mod->getContext ()),
1330
+ getCharPtrDebugType (), def, mod, getter);
1353
1331
}
1354
1332
1355
1333
void emitConfigParserFunctions (
0 commit comments