Skip to content

Commit

Permalink
Merge pull request #389 from Wiguwbe/master
Browse files Browse the repository at this point in the history
Add support for running MIR binaries, useful for binfmt_misc
  • Loading branch information
vnmakarov authored Feb 1, 2024
2 parents 2944a3e + 7110b21 commit 12a5d15
Show file tree
Hide file tree
Showing 5 changed files with 424 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@
*.so
*.so.*
*.a
/mir-run
/build
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ add_executable (c2m "c2mir/c2mir-driver.c")
target_include_directories(c2m PRIVATE ${PROJECT_SOURCE_DIR})
target_link_libraries(c2m mir ${CMAKE_DL_LIBS} )

# ------------------ MIR RUN ----------------------

add_executable (mir-run "mir-run.c")
target_include_directories (mir-run PRIVATE ${PROJECT_SOURCE_DIR})
target_link_libraries(mir-run mir ${CMAKE_DL_LIBS} )

# ------------------ MIR utils --------------------
add_executable (m2b "mir-utils/m2b.c")
target_include_directories(m2b PRIVATE ${PROJECT_SOURCE_DIR})
Expand Down
16 changes: 15 additions & 1 deletion GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ L2M_EXE += $(BUILD_DIR)/l2m$(EXE)
L2M_TEST += l2m-test$(EXE)
endif

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

Q=@

Expand Down Expand Up @@ -235,6 +235,20 @@ clean-c2m:

-include $(C2M_BUILD:.$(OBJSUFF)=.d)

# ------------------ MIR RUN ----------------------

MIR_RUN_SRC:=$(SRC_DIR)/mir-run.c
MIR_RUN_BUILD:=$(MIR_RUN_SRC:$(SRC_DIR)/%.c=$(BUILD_DIR)/%.$(OBJSUFF))

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

.PHONY: clean-mir-run
clean-mir-run:
$(RM) $(MIR_RUN_BUILD) $(MIR_RUN_BUILD:.$(OBJSUFF)=.d)

-include $(MIR_RUN_BUILD:.$(OBJSUFF)=.d)

# ------------------ L2M --------------------------
L2M_SRC:=$(SRC_DIR)/llvm2mir/llvm2mir.c $(SRC_DIR)/llvm2mir/llvm2mir-driver.c
L2M_BUILD:=$(L2M_SRC:$(SRC_DIR)/%.c=$(BUILD_DIR)/%.$(OBJSUFF))
Expand Down
45 changes: 39 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
no warranty that MIR will not be changed in the future and the
code will work for any tests except ones given here and on platforms
other than x86_64 Linux/OSX, aarch64 Linux/OSX(Apple M1), and ppc64be/ppc64le/s390x/riscv64 Linux**

## MIR
* MIR is strongly typed IR
* MIR can represent machine 32-bit and 64-bit insns of different architectures
Expand Down Expand Up @@ -169,7 +169,7 @@ ex100: func v, 0
* After linking, you can interpret functions from the modules or call machine code
for the functions generated with MIR JIT compiler (generator). What way the function can be executed
is usually defined by set up interface. How the generated code is produced (lazily on the first call or ahead of time)
can be also dependent on the interface
can be also dependent on the interface
* Running code from the above example could look like the following (here `m1` and `m2` are modules
`m_sieve` and `m_e100`, `func` is function `ex100`, `sieve` is function `sieve`):
```c
Expand All @@ -184,6 +184,39 @@ ex100: func v, 0
/* or ((void (*) (void)) func->addr) (); to call interpr. or gen. code through the interface */
```
### Running through `binfmt_misc`
The `mir-run` binary is prepared to be used from `binfmt_misc` with the
following line (example):
```bash
line=:mir:M::MIR::/usr/local/bin/mir-run:P
echo $line > /proc/sys/fs/binfmt_misc/register
```

> Do adapt the mir-run binary path to your system, that is the default one
And run with
```bash
c2m your-file.c -o your-file
chmod +x your-file
./your-file your args
```

The executable is "configurable" with environment variables:

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


> Due to the tied nature of `mir-run` with `binfmt_misc`, it may be a bit weird
> to call `mir-run` directly.
> The `P` flag on the binfmt_misc passes an extra argument with the full path
> to the MIR binary.
## The current state of MIR project

![Current MIR](mir3.svg)
Expand All @@ -209,7 +242,7 @@ ex100: func v, 0
* Performance minded porting MIR JIT compiler to 32-bit targets will need an implementation of
additional small analysis pass to get info what 64-bit variables are used only
in 32-bit instructions

## MIR JIT compiler
* Compiler **Performance Goals** relative to GCC -O2:
* 70% of generated code speed
Expand Down Expand Up @@ -257,7 +290,7 @@ ex100: func v, 0
* **Combine** (code selection): merging data-depended insns into one
* **Dead Code Elimination**: removing insns with unused outputs
* **Generate Machine Insns**: run machine-dependent code creating machine insns

## C to MIR translation
* Currently work on 2 different ways of the translation are ongoing
* Implementation of a small C11 (2011 ANSI C standard) to MIR compiler.
Expand All @@ -279,13 +312,13 @@ ex100: func v, 0
* Files `mir-gen-x86_64.c`, `mir-gen-aarch64.c`, `mir-gen-ppc64.c`, `mir-gen-s390x.c`,
and `mir-gen-riscv.c` is machine dependent code of JIT compiler
* Files `mir-<target>.c` contain simple machine dependent code common for interpreter and
JIT compiler
JIT compiler
* Files `mir2c/mir2c.h` and `mir2c/mir2c.c` contain code for MIR to C compiler
* Files `c2mir/c2mir.h`, `c2mir/c2mir.c`, `c2mir/c2mir-driver.c`, and `c2mir/mirc.h` contain code for
C to MIR compiler. Files in directories `c2mir/x86_64` and `c2mir/aarch64`, `c2mir/ppc64`, `c2mir/s390x`,
and `c2mir/riscv` contain correspondingly x86_64, aarch64, ppc64, s390x, and riscv machine-dependent
code for C to MIR compiler

## Playing with current MIR project code
* MIR project is far away from any serious usage
* The current code can be used only to familiarize future users with the project
Expand Down
Loading

0 comments on commit 12a5d15

Please sign in to comment.