standalone: Add ability to execute via lli

This commit is contained in:
David Mak 2023-09-19 07:35:24 +08:00
parent c7d5d75014
commit 5ab8d751ed
2 changed files with 105 additions and 0 deletions

View File

@ -0,0 +1,84 @@
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define min(a, b) \
({ \
__typeof__(a) _a = (a); \
__typeof__(b) _b = (b); \
_a < _b ? _a : _b; \
})
#if __SIZEOF_POINTER__ == 8
#define usize uint64_t
#elif __SIZEOF_POINTER__ == 4
#define usize uint32_t
#elif __SIZEOF_POINTER__ == 2
#define usize uint16_t
#endif
void output_int32(const int32_t x) {
printf("%d\n", x);
}
void output_int64(const int64_t x) {
printf("%ld\n", x);
}
void output_uint32(const uint32_t x) {
printf("%d\n", x);
}
void output_uint64(const uint64_t x) {
printf("%ld\n", x);
}
void output_asciiart(const int32_t x) {
const char* chars = " .,-:;i+hHM$*#@ ";
if (x < 0) {
fputc('\n', stdout);
} else {
fputc(chars[x], stdout);
}
}
struct cslice_int32 {
const int32_t* data;
usize len;
};
void output_int32_list(struct cslice_int32* slice) {
fputc('[', stdout);
for (usize i = 0; i < slice->len; ++i) {
if (i == slice->len - 1) {
printf("%d", slice->data[i]);
} else {
printf("%d, ", slice->data[i]);
}
}
puts("]");
}
uint32_t __nac3_personality(
__attribute__((unused)) uint32_t state,
__attribute__((unused)) uint32_t exception_object,
__attribute__((unused)) uint32_t context) {
assert(false && "__nac3_personality not implemented");
exit(101);
__builtin_unreachable();
}
uint32_t __nac3_raise(uint32_t state, uint32_t exception_object, uint32_t context) {
printf("__nac3_raise(state: %x, exception_object: %x, context: %x\n", state, exception_object, context);
exit(101);
__builtin_unreachable();
}
extern int32_t run();
int main() {
run();
}

View File

@ -0,0 +1,21 @@
#!/usr/bin/env bash
set -e
if [ -z "$1" ]; then
echo "No argument supplied"
exit 1
fi
if [ -e ../../target/release/nac3standalone ]; then
nac3standalone=../../target/release/nac3standalone
else
# used by Nix builds
nac3standalone=../../target/x86_64-unknown-linux-gnu/release/nac3standalone
fi
rm -f *.o *.bc
$nac3standalone --emit-llvm "$@"
gcc -c -std=c11 -Wall -Wextra -pedantic-errors -Werror=pedantic -O3 -o demo.o demo.c
clang -S -Wall -Wextra -O3 -emit-llvm -o irrt.bc ../../nac3core/src/codegen/irrt/irrt.c
lli --extra-object demo.o --extra-module irrt.bc main.bc