Skip to content

Commit 9b7aa03

Browse files
committed
Rename mir-run... to mir-bin-run... Add mir-bin-run-test. Format mir-bin-run.c, make lazy default, rename jit to gen.
Modify description for mir-bin-run.
1 parent 12a5d15 commit 9b7aa03

File tree

3 files changed

+78
-81
lines changed

3 files changed

+78
-81
lines changed

GNUmakefile

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ L2M_EXE += $(BUILD_DIR)/l2m$(EXE)
129129
L2M_TEST += l2m-test$(EXE)
130130
endif
131131

132-
EXECUTABLES=$(BUILD_DIR)/c2m$(EXE) $(BUILD_DIR)/m2b$(EXE) $(BUILD_DIR)/b2m$(EXE) $(BUILD_DIR)/b2ctab$(EXE) $(L2M_EXE) $(BUILD_DIR)/mir-run$(EXE)
132+
EXECUTABLES=$(BUILD_DIR)/c2m$(EXE) $(BUILD_DIR)/m2b$(EXE) $(BUILD_DIR)/b2m$(EXE) $(BUILD_DIR)/b2ctab$(EXE) $(L2M_EXE) $(BUILD_DIR)/mir-bin-run$(EXE)
133133

134134
Q=@
135135

@@ -180,7 +180,7 @@ endif
180180
clean: clean-mir clean-c2m clean-utils clean-l2m clean-adt-tests clean-mir-tests clean-mir2c-test clean-bench
181181
$(RM) $(EXECUTABLES) $(BUILD_DIR)/libmir.$(LIBSUFF) $(BUILD_DIR)/$(SOLIB)
182182

183-
test: readme-example-test c2mir-test
183+
test: readme-example-test mir-bin-run-test c2mir-test
184184

185185
test-all: adt-test simplify-test io-test scan-test mir2c-test $(L2M-TEST) test
186186

@@ -237,14 +237,14 @@ clean-c2m:
237237

238238
# ------------------ MIR RUN ----------------------
239239

240-
MIR_RUN_SRC:=$(SRC_DIR)/mir-run.c
240+
MIR_RUN_SRC:=$(SRC_DIR)/mir-bin-run.c
241241
MIR_RUN_BUILD:=$(MIR_RUN_SRC:$(SRC_DIR)/%.c=$(BUILD_DIR)/%.$(OBJSUFF))
242242

243-
$(BUILD_DIR)/mir-run$(EXE): $(MIR_RUN_BUILD) $(BUILD_DIR)/libmir.$(LIBSUFF) | $(BUILD_DIR)
243+
$(BUILD_DIR)/mir-bin-run$(EXE): $(MIR_RUN_BUILD) $(BUILD_DIR)/libmir.$(LIBSUFF) | $(BUILD_DIR)
244244
$(LINK) $^ $(LDLIBS) $(EXEO)$@ $(BUILD_DIR)/libmir.$(LIBSUFF)
245245

246-
.PHONY: clean-mir-run
247-
clean-mir-run:
246+
.PHONY: clean-mir-bin-run
247+
clean-mir-bin-run:
248248
$(RM) $(MIR_RUN_BUILD) $(MIR_RUN_BUILD:.$(OBJSUFF)=.d)
249249

250250
-include $(MIR_RUN_BUILD:.$(OBJSUFF)=.d)
@@ -539,6 +539,17 @@ gen-test16: $(BUILD_DIR)/run-test$(EXE)
539539
clean-mir-gen-tests:
540540
$(RM) $(BUILD_DIR)/mir-tests/gen-loop-test$(EXE) $(BUILD_DIR)/mir-tests/issue219$(EXE)
541541

542+
# ------------------ MIR run tests --------------------------
543+
544+
mir-bin-run-test: $(BUILD_DIR)/mir-bin-run$(EXE) $(BUILD_DIR)/c2m$(EXE)
545+
$(BUILD_DIR)/c2m$(EXE) -c $(SRC_DIR)/sieve.c
546+
$(BUILD_DIR)/mir-bin-run$(EXE) `pwd`/sieve.bmir sieve.bmir
547+
MIR_TYPE=interp time $(BUILD_DIR)/mir-bin-run$(EXE) `pwd`/sieve.bmir sieve.bmir
548+
MIR_TYPE=gen time $(BUILD_DIR)/mir-bin-run$(EXE) `pwd`/sieve.bmir sieve.bmir
549+
MIR_TYPE=lazy time $(BUILD_DIR)/mir-bin-run$(EXE) `pwd`/sieve.bmir sieve.bmir
550+
rm sieve.bmir
551+
552+
542553
# ------------------ readme example test ----------------
543554

544555
.PHONY: readme-example-test clean-readme-example-test

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -184,17 +184,17 @@ ex100: func v, 0
184184
/* or ((void (*) (void)) func->addr) (); to call interpr. or gen. code through the interface */
185185
```
186186
187-
### Running through `binfmt_misc`
187+
### Running binary MIR files on Linux through `binfmt_misc`
188188
189-
The `mir-run` binary is prepared to be used from `binfmt_misc` with the
189+
The `mir-bin-run` binary is prepared to be used from `binfmt_misc` with the
190190
following line (example):
191191
192192
```bash
193-
line=:mir:M::MIR::/usr/local/bin/mir-run:P
193+
line=:mir:M::MIR::/usr/local/bin/mir-bin-run:P
194194
echo $line > /proc/sys/fs/binfmt_misc/register
195195
```
196196

197-
> Do adapt the mir-run binary path to your system, that is the default one
197+
> Do adapt the mir-bin-run binary path to your system, that is the default one
198198
199199
And run with
200200
```bash
@@ -205,15 +205,15 @@ chmod +x your-file
205205

206206
The executable is "configurable" with environment variables:
207207

208-
- `MIR_TYPE` sets the interface for code execution: `interp` (default),
209-
`jit` (for generation) and `lazy` (for lazy generation);
210-
- `MIR_LIBS` (colon separated list) defines a list of extra libraries to load;
211-
- `MIR_LIB_DIRS` or `LD_LIBRARY_PATH` (colon separated list) defines an extra list
208+
* `MIR_TYPE` sets the interface for code execution: `interp` (for interpretation),
209+
`jit` (for generation) and `lazy` (for lazy generation, default);
210+
* `MIR_LIBS` (colon separated list) defines a list of extra libraries to load;
211+
* `MIR_LIB_DIRS` or `LD_LIBRARY_PATH` (colon separated list) defines an extra list
212212
of directories to search the libraries on.
213213

214214

215-
> Due to the tied nature of `mir-run` with `binfmt_misc`, it may be a bit weird
216-
> to call `mir-run` directly.
215+
> Due to the tied nature of `mir-bin-run` with `binfmt_misc`, it may be a bit weird
216+
> to call `mir-bin-run` directly.
217217
> The `P` flag on the binfmt_misc passes an extra argument with the full path
218218
> to the MIR binary.
219219

mir-run.c renamed to mir-bin-run.c

Lines changed: 51 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44
#include <alloca.h>
55
#include <dlfcn.h>
66
#include <sys/stat.h>
7-
#include "mir-gen.h" // mir.h gets included as well
7+
#include "mir-gen.h" // mir.h gets included as well
88

99
#define MIR_TYPE_INTERP 1
1010
#define MIR_TYPE_INTERP_NAME "interp"
11-
#define MIR_TYPE_JIT 2
12-
#define MIR_TYPE_JIT_NAME "jit"
11+
#define MIR_TYPE_GEN 2
12+
#define MIR_TYPE_GEN_NAME "gen"
1313
#define MIR_TYPE_LAZY 3
1414
#define MIR_TYPE_LAZY_NAME "lazy"
1515

16-
#define MIR_TYPE_DEFAULT MIR_TYPE_INTERP
16+
#define MIR_TYPE_DEFAULT MIR_TYPE_LAZY
1717

1818
#define MIR_ENV_VAR_LIB_DIRS "MIR_LIB_DIRS"
1919
#define MIR_ENV_VAR_EXTRA_LIBS "MIR_LIBS"
@@ -34,11 +34,13 @@ static lib_t std_libs[]
3434
static const char *std_lib_dirs[] = {"/lib", "/lib32"};
3535
#elif UINTPTR_MAX == 0xffffffffffffffff
3636
#if defined(__x86_64__)
37-
static lib_t std_libs[]
38-
= {{"/lib64/libc.so.6", NULL}, {"/lib/x86_64-linux-gnu/libc.so.6", NULL},
39-
{"/lib64/libm.so.6", NULL}, {"/lib/x86_64-linux-gnu/libm.so.6", NULL},
40-
{"/usr/lib64/libpthread.so.0", NULL}, {"/lib/x86_64-linux-gnu/libpthread.so.0", NULL},
41-
{"/usr/lib/libc.so", NULL}};
37+
static lib_t std_libs[] = {{"/lib64/libc.so.6", NULL},
38+
{"/lib/x86_64-linux-gnu/libc.so.6", NULL},
39+
{"/lib64/libm.so.6", NULL},
40+
{"/lib/x86_64-linux-gnu/libm.so.6", NULL},
41+
{"/usr/lib64/libpthread.so.0", NULL},
42+
{"/lib/x86_64-linux-gnu/libpthread.so.0", NULL},
43+
{"/usr/lib/libc.so", NULL}};
4244
static const char *std_lib_dirs[] = {"/lib64", "/lib/x86_64-linux-gnu"};
4345
#elif (__aarch64__)
4446
static lib_t std_libs[]
@@ -111,7 +113,6 @@ static const char *lib_suffix = ".dll";
111113
#define dlsym(h, s) GetProcAddress (h, s)
112114
#endif
113115

114-
115116
static void close_std_libs (void) {
116117
for (int i = 0; i < sizeof (std_libs) / sizeof (lib_t); i++)
117118
if (std_libs[i].handler != NULL) dlclose (std_libs[i].handler);
@@ -222,20 +223,19 @@ static void *import_resolver (const char *name) {
222223
return sym;
223224
}
224225

225-
void lib_dirs_from_env_var(const char *env_var) {
226-
const char *var_value = getenv(env_var);
227-
if (var_value == NULL || var_value[0] == '\0')
228-
return;
226+
void lib_dirs_from_env_var (const char *env_var) {
227+
const char *var_value = getenv (env_var);
228+
if (var_value == NULL || var_value[0] == '\0') return;
229229

230230
// copy to an allocated buffer
231-
int value_len = strlen(var_value);
232-
char *value = (char*)malloc(value_len+1);
233-
strcpy(value, var_value);
231+
int value_len = strlen (var_value);
232+
char *value = (char *) malloc (value_len + 1);
233+
strcpy (value, var_value);
234234

235235
// colon separated list
236236
char *value_ptr = value;
237237
char *colon = NULL;
238-
while ((colon = strchr(value_ptr, ':')) != NULL) {
238+
while ((colon = strchr (value_ptr, ':')) != NULL) {
239239
colon[0] = '\0';
240240
VARR_PUSH (char_ptr_t, lib_dirs, value_ptr);
241241
// goto next
@@ -246,76 +246,69 @@ void lib_dirs_from_env_var(const char *env_var) {
246246
VARR_PUSH (char_ptr_t, lib_dirs, value_ptr);
247247
}
248248

249-
int get_mir_type(void) {
250-
const char *type_value = getenv(MIR_ENV_VAR_TYPE);
251-
if (type_value == NULL || type_value[0] == '\0')
252-
return MIR_TYPE_DEFAULT;
249+
int get_mir_type (void) {
250+
const char *type_value = getenv (MIR_ENV_VAR_TYPE);
251+
if (type_value == NULL || type_value[0] == '\0') return MIR_TYPE_DEFAULT;
253252

254-
if (strcmp(type_value, MIR_TYPE_INTERP_NAME) == 0)
255-
return MIR_TYPE_INTERP;
253+
if (strcmp (type_value, MIR_TYPE_INTERP_NAME) == 0) return MIR_TYPE_INTERP;
256254

257-
if (strcmp(type_value, MIR_TYPE_JIT_NAME) == 0)
258-
return MIR_TYPE_JIT;
255+
if (strcmp (type_value, MIR_TYPE_GEN_NAME) == 0) return MIR_TYPE_GEN;
259256

260-
if (strcmp(type_value, MIR_TYPE_LAZY_NAME) == 0)
261-
return MIR_TYPE_LAZY;
257+
if (strcmp (type_value, MIR_TYPE_LAZY_NAME) == 0) return MIR_TYPE_LAZY;
262258

263-
fprintf(stderr, "warning: unknown MIR_TYPE '%s', using default one\n", type_value);
259+
fprintf (stderr, "warning: unknown MIR_TYPE '%s', using default one\n", type_value);
264260
return MIR_TYPE_DEFAULT;
265261
}
266262

267-
void open_extra_libs(void) {
268-
const char *var_value = getenv(MIR_ENV_VAR_EXTRA_LIBS);
269-
if (var_value == NULL || var_value[0] == '\0')
270-
return;
263+
void open_extra_libs (void) {
264+
const char *var_value = getenv (MIR_ENV_VAR_EXTRA_LIBS);
265+
if (var_value == NULL || var_value[0] == '\0') return;
271266

272-
int value_len = strlen(var_value);
273-
char *value = (char*)malloc(value_len+1);
274-
strcpy(value, var_value);
267+
int value_len = strlen (var_value);
268+
char *value = (char *) malloc (value_len + 1);
269+
strcpy (value, var_value);
275270

276271
char *value_ptr = value;
277272
char *colon = NULL;
278-
while ((colon = strchr(value_ptr, ':')) != NULL) {
273+
while ((colon = strchr (value_ptr, ':')) != NULL) {
279274
colon[0] = '\0';
280-
process_extra_lib(value_ptr);
275+
process_extra_lib (value_ptr);
281276

282277
value_ptr = colon + 1;
283278
}
284-
process_extra_lib(value_ptr);
279+
process_extra_lib (value_ptr);
285280
}
286281

287-
288282
int main (int argc, char **argv, char **envp) {
289-
290283
// from binfmt_misc we expect the arguments to be:
291284
// `mir-run /full/path/to/mir-binary mir-binary <args...>`
292285
if (argc < 3) {
293-
fprintf(stderr, "usage: %s <full-path> <name> [<args>...]\n", argv[0]);
286+
fprintf (stderr, "usage: %s <full-path> <name> [<args>...]\n", argv[0]);
294287
return 1;
295288
}
296289

297-
int mir_type = get_mir_type();
290+
int mir_type = get_mir_type ();
298291

299292
MIR_val_t val;
300293
int exit_code;
301294

302295
VARR_CREATE (char, temp_string, 0);
303296
VARR_CREATE (lib_t, extra_libs, 16);
304297
VARR_CREATE (char_ptr_t, lib_dirs, 16);
305-
for(int i=0; i < sizeof(std_lib_dirs) / sizeof(char_ptr_t); i++)
298+
for (int i = 0; i < sizeof (std_lib_dirs) / sizeof (char_ptr_t); i++)
306299
VARR_PUSH (char_ptr_t, lib_dirs, std_lib_dirs[i]);
307-
lib_dirs_from_env_var("LD_LIBRARY_PATH");
308-
lib_dirs_from_env_var(MIR_ENV_VAR_LIB_DIRS);
300+
lib_dirs_from_env_var ("LD_LIBRARY_PATH");
301+
lib_dirs_from_env_var (MIR_ENV_VAR_LIB_DIRS);
309302

310303
MIR_item_t main_func = NULL;
311304

312-
MIR_context_t mctx = MIR_init();
313-
FILE *mir_file = fopen(argv[1], "r");
305+
MIR_context_t mctx = MIR_init ();
306+
FILE *mir_file = fopen (argv[1], "r");
314307
if (!mir_file) {
315-
fprintf(stderr, "failed to open file '%s'\n", argv[1]);
308+
fprintf (stderr, "failed to open file '%s'\n", argv[1]);
316309
return 1;
317310
}
318-
MIR_read(mctx, mir_file);
311+
MIR_read (mctx, mir_file);
319312

320313
for (MIR_module_t module = DLIST_HEAD (MIR_module_t, *MIR_get_module_list (mctx)); module != NULL;
321314
module = DLIST_NEXT (MIR_module_t, module)) {
@@ -336,27 +329,20 @@ int main (int argc, char **argv, char **envp) {
336329

337330
if (mir_type == MIR_TYPE_INTERP) {
338331
MIR_link (mctx, MIR_set_interp_interface, import_resolver);
339-
MIR_interp (
340-
mctx, main_func, &val, 3,
341-
(MIR_val_t){.i = (argc-2)},
342-
(MIR_val_t){.a = (void*)(argv+2)},
343-
(MIR_val_t){.a = (void*)envp}
344-
);
332+
MIR_interp (mctx, main_func, &val, 3, (MIR_val_t){.i = (argc - 2)},
333+
(MIR_val_t){.a = (void *) (argv + 2)}, (MIR_val_t){.a = (void *) envp});
345334
exit_code = val.i;
346335
} else {
347336
MIR_gen_init (mctx, 1);
348-
MIR_link (mctx,
349-
mir_type == MIR_TYPE_JIT
350-
? MIR_set_gen_interface
351-
: MIR_set_lazy_gen_interface,
337+
MIR_link (mctx, mir_type == MIR_TYPE_GEN ? MIR_set_gen_interface : MIR_set_lazy_gen_interface,
352338
import_resolver);
353-
uint64_t (*fun_addr)(int, char**, char**) = MIR_gen(mctx, 0, main_func);
354-
exit_code = fun_addr(argc-2, argv+2, envp);
355-
MIR_gen_finish(mctx);
339+
uint64_t (*fun_addr) (int, char **, char **) = MIR_gen (mctx, 0, main_func);
340+
exit_code = fun_addr (argc - 2, argv + 2, envp);
341+
MIR_gen_finish (mctx);
356342
}
357343
MIR_finish (mctx);
358-
close_extra_libs();
359-
close_std_libs();
344+
close_extra_libs ();
345+
close_std_libs ();
360346

361347
return exit_code;
362348
}

0 commit comments

Comments
 (0)