Skip to content

Commit 0d510ba

Browse files
committed
[[ lc-compile ]] Add --forcebuiltins option
This new option allows any C foreign handler to be generated as a builtin shim when output is in `--outputauxc` mode. This option also sets the module initializer and module finalizer functions to nullptr.
1 parent 7d35bb8 commit 0d510ba

File tree

3 files changed

+94
-21
lines changed

3 files changed

+94
-21
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# LiveCode Builder Tools
2+
## lc-compile
3+
### Command-line interface
4+
5+
* The new `--forcebuiltins` command line flag can be used in combination with
6+
the `--outputauxc OUTFILE` option to generate shims for C foreign bindings.
7+
Use the output file to link with any static libraries used by modules on iOS
8+
to create a lcext binary and the shims will ensure that required objects from
9+
the static libraries are included in the resulting binary.

toolchain/lc-compile/src/emit.cpp

Lines changed: 72 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ extern "C" void OutputBeginManifest(void);
173173
extern "C" int IsBootstrapCompile(void);
174174
extern "C" int IsNotBytecodeOutput(void);
175175

176+
extern "C" int ForceCBindingsAsBuiltins(void);
177+
176178
extern "C" void DependStart(void);
177179
extern "C" void DependFinish(void);
178180
extern "C" void DependDefineMapping(NameRef module_name, const char *source_file);
@@ -319,17 +321,31 @@ static bool EmitEmittedModules(void)
319321
for(EmittedModule *t_module = s_emitted_modules; t_module != nullptr; t_module = t_module->next)
320322
{
321323
const char *t_modified_name = t_module->modified_name;
322-
324+
323325
if (0 > fprintf(t_file,
324-
"static __builtin_module_info __%s_module_info =\n{\n 0,\n 0,\n %s_module_data,\n sizeof(%s_module_data),\n %s_Initialize,\n %s_Finalize,\n __builtin_ordinal_map\n};\n",
325-
t_modified_name,
326-
t_modified_name,
326+
"static __builtin_module_info __%s_module_info =\n{\n 0,\n 0,\n %s_module_data,\n sizeof(%s_module_data),\n",
327327
t_modified_name,
328328
t_modified_name,
329329
t_modified_name))
330330
return false;
331331

332-
if (0 > fprintf(t_file, "__builtin_module_loader __%s_loader(&__%s_module_info);\n\n",
332+
if (!ForceCBindingsAsBuiltins())
333+
{
334+
if (0 > fprintf(t_file,
335+
" %s_Initialize,\n %s_Finalize,\n",
336+
t_modified_name,
337+
t_modified_name))
338+
return false;
339+
}
340+
else
341+
{
342+
if (0 > fprintf(t_file,
343+
" nullptr,\n nullptr,\n"))
344+
return false;
345+
}
346+
347+
if (0 > fprintf(t_file,
348+
" __builtin_ordinal_map\n};\n__builtin_module_loader __%s_loader(&__%s_module_info);\n\n",
333349
t_modified_name,
334350
t_modified_name))
335351
return false;
@@ -355,9 +371,9 @@ struct EmittedBuiltin
355371
static EmittedBuiltin *s_emitted_builtins = nullptr;
356372
static uindex_t s_emitted_builtin_count = 0;
357373

358-
static MCStringRef EmittedBuiltinAdd(NameRef p_symbol_name, uindex_t p_type_index)
374+
static MCStringRef EmittedBuiltinAdd(NameRef p_symbol_name, uindex_t p_type_index, bool p_force)
359375
{
360-
if (!OutputFileAsC || OutputFileAsAuxC)
376+
if (!p_force && (!OutputFileAsC || OutputFileAsAuxC))
361377
{
362378
return MCNameGetString(to_mcnameref(p_symbol_name));
363379
}
@@ -808,17 +824,20 @@ EmitEndModuleOutputC (NameRef p_module_name,
808824
if (t_modified_name[i] == '.')
809825
t_modified_name[i] = '_';
810826

811-
// Emit the initializer reference.
812-
if (0 > fprintf(s_output_code_file,
813-
"extern \"C\" bool %s_Initialize(void);\n",
814-
t_modified_name))
815-
goto error_cleanup;
816-
817-
// Emit the finalizer reference.
818-
if (0 > fprintf(s_output_code_file,
819-
"extern \"C\" void %s_Finalize(void);\n",
820-
t_modified_name))
821-
goto error_cleanup;
827+
if (!ForceCBindingsAsBuiltins())
828+
{
829+
// Emit the initializer reference.
830+
if (0 > fprintf(s_output_code_file,
831+
"extern \"C\" bool %s_Initialize(void);\n",
832+
t_modified_name))
833+
goto error_cleanup;
834+
835+
// Emit the finalizer reference.
836+
if (0 > fprintf(s_output_code_file,
837+
"extern \"C\" void %s_Finalize(void);\n",
838+
t_modified_name))
839+
goto error_cleanup;
840+
}
822841

823842
if (0 > fprintf(t_file, "static unsigned char %s_module_data[] = {", t_modified_name))
824843
goto error_cleanup;
@@ -1107,10 +1126,42 @@ void EmitVariableDefinition(intptr_t p_index, PositionRef p_position, NameRef p_
11071126

11081127
static void EmitForeignHandlerDefinition(intptr_t p_index, PositionRef p_position, NameRef p_name, intptr_t p_type_index, intptr_t p_binding, bool p_force_builtin)
11091128
{
1129+
MCAutoStringRef t_binding(to_mcstringref(p_binding));
1130+
MCAutoCustomPointer<struct MCScriptForeignHandlerInfo, MCScriptForeignHandlerInfoRelease> t_info;
1131+
if (!MCScriptForeignHandlerInfoParse(*t_binding, &t_info))
1132+
{
1133+
return;
1134+
}
1135+
1136+
bool t_force_c_builtin = t_info->language == kMCScriptForeignHandlerLanguageC &&
1137+
ForceCBindingsAsBuiltins();
1138+
11101139
MCAutoStringRef t_binding_str;
1111-
if (strcmp((const char *)p_binding, "<builtin>") == 0 || p_force_builtin)
1140+
if (t_info->language == kMCScriptForeignHandlerLanguageBuiltinC ||
1141+
p_force_builtin ||
1142+
t_force_c_builtin)
11121143
{
1113-
t_binding_str = EmittedBuiltinAdd(p_force_builtin ? nameref_from_cstring((const char *)p_binding) : p_name, p_type_index);
1144+
NameRef t_symbol_name;
1145+
if (t_info->language == kMCScriptForeignHandlerLanguageBuiltinC)
1146+
{
1147+
t_symbol_name = p_name;
1148+
}
1149+
else if (p_force_builtin)
1150+
{
1151+
t_symbol_name = nameref_from_cstring((const char *)p_binding);
1152+
}
1153+
else
1154+
{
1155+
MCAutoStringRefAsCString t_symbol_c_string;
1156+
if (!t_symbol_c_string . Lock(t_info->c.function))
1157+
{
1158+
return;
1159+
}
1160+
1161+
t_symbol_name = nameref_from_cstring(*t_symbol_c_string);
1162+
}
1163+
1164+
t_binding_str = EmittedBuiltinAdd(t_symbol_name, p_type_index, ForceCBindingsAsBuiltins());
11141165
}
11151166
else
11161167
t_binding_str = to_mcstringref(p_binding);
@@ -1386,7 +1437,7 @@ void EmitForeignType(intptr_t p_binding, intptr_t& r_type_index)
13861437
{
13871438
uindex_t t_type_index;
13881439
MCStringRef t_binding_str =
1389-
EmittedBuiltinAdd(nameref_from_mcstringref(to_mcstringref(p_binding)), UINDEX_MAX);
1440+
EmittedBuiltinAdd(nameref_from_mcstringref(to_mcstringref(p_binding)), UINDEX_MAX, false);
13901441

13911442
MCScriptAddForeignTypeToModule(s_builder, t_binding_str, t_type_index);
13921443
r_type_index = t_type_index;

toolchain/lc-compile/src/main.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ extern int OutputFileAsC;
3434
extern int OutputFileAsAuxC;
3535
extern int OutputFileAsBytecode;
3636

37+
static int s_force_c_builtins = 0;
38+
39+
int ForceCBindingsAsBuiltins(void)
40+
{
41+
return s_force_c_builtins;
42+
}
43+
3744
int IsBootstrapCompile(void)
3845
{
3946
return s_is_bootstrap;
@@ -123,6 +130,7 @@ usage(int status)
123130
" compiled in, but only if they need recompiling.\n"
124131
" --manifest MANIFEST Filename for generated manifest.\n"
125132
" --interface INTERFACE Filename for generated interface.\n"
133+
" --forcebuiltins Generate c bindings as builtin shims for auxc output.\n"
126134
" -Werror Turn all warnings into errors.\n"
127135
" -v, --verbose Output extra debugging information.\n"
128136
" -h, --help Print this message.\n"
@@ -240,6 +248,11 @@ static void full_main(int argc, char *argv[])
240248
usage(0);
241249
continue;
242250
}
251+
if (0 == strcmp(opt, "--forcebuiltins"))
252+
{
253+
s_force_c_builtins = 1;
254+
continue;
255+
}
243256
if (0 == strcmp(opt, "--"))
244257
{
245258
end_of_args = 1;

0 commit comments

Comments
 (0)