Add x86_64-linux-gnu emulator

I wanted a tiny scriptable meltdown proof way to run userspace programs
and visualize how program execution impacts memory. It helps to explain
how things like Actually Portable Executable works. It can show you how
the GCC generated code is going about manipulating matrices and more. I
didn't feel fully comfortable with Qemu and Bochs because I'm not smart
enough to understand them. I wanted something like gVisor but with much
stronger levels of assurances. I wanted a single binary that'll run, on
all major operating systems with an embedded GPL barrier ZIP filesystem
that is tiny enough to transpile to JavaScript and run in browsers too.

https://justine.storage.googleapis.com/emulator625.mp4
This commit is contained in:
Justine Tunney 2020-08-25 04:23:25 -07:00
parent 467504308a
commit f4f4caab0e
1052 changed files with 65667 additions and 7825 deletions

View file

@ -29,9 +29,11 @@
#include "libc/fmt/paland.inc"
#include "libc/fmt/palandprintf.h"
uintmax_t __udivmodti4(uintmax_t, uintmax_t, uintmax_t *);
static int ntoaformat(int out(int, void *), void *arg, char *buf, unsigned len,
bool negative, unsigned log2base, unsigned prec,
unsigned width, unsigned flags) {
unsigned width, unsigned char flags) {
unsigned i, idx;
idx = 0;
@ -101,9 +103,10 @@ static int ntoaformat(int out(int, void *), void *arg, char *buf, unsigned len,
return 0;
}
static int ntoa2(int out(int, void *), void *arg, uintmax_t value, bool neg,
unsigned log2base, unsigned prec, unsigned width,
unsigned flags, const char *alphabet) {
int ntoa2(int out(int, void *), void *arg, uintmax_t value, bool neg,
unsigned log2base, unsigned prec, unsigned width, unsigned flags,
const char *alphabet) {
uintmax_t remainder;
unsigned len, count, digit;
char buf[PRINTF_NTOA_BUFFER_SIZE];
len = 0;
@ -112,12 +115,13 @@ static int ntoa2(int out(int, void *), void *arg, uintmax_t value, bool neg,
count = 0;
do {
assert(len < PRINTF_NTOA_BUFFER_SIZE);
if (log2base) {
if (!log2base) {
value = __udivmodti4(value, 10, &remainder);
digit = remainder;
} else {
digit = value;
digit &= (1u << log2base) - 1;
value >>= log2base;
} else {
value = div10(value, &digit);
}
if ((flags & FLAGS_GROUPING) && count == 3) {
buf[len++] = ',';
@ -132,12 +136,12 @@ static int ntoa2(int out(int, void *), void *arg, uintmax_t value, bool neg,
}
int ntoa(int out(int, void *), void *arg, va_list va, unsigned char signbit,
unsigned long log2base, unsigned long precision, unsigned long width,
unsigned long flags, const char *alphabet) {
bool negative;
unsigned long log2base, unsigned long prec, unsigned long width,
unsigned char flags, const char *lang) {
bool neg;
uintmax_t value, sign;
/* ignore '0' flag when precision is given */
/* ignore '0' flag when prec is given */
if (flags & FLAGS_PRECISION) {
flags &= ~FLAGS_ZEROPAD;
}
@ -153,24 +157,22 @@ int ntoa(int out(int, void *), void *arg, va_list va, unsigned char signbit,
value = va_arg(va, uint64_t);
}
negative = false;
sign = (uintmax_t)1 << signbit;
if (value > (sign | (sign - 1))) erange();
neg = 0;
sign = 1;
sign <<= signbit;
value &= sign | (sign - 1);
if (flags & FLAGS_ISSIGNED) {
if (value != sign) {
if (value & sign) {
value = ~value + 1;
negative = true;
value &= sign | (sign - 1);
neg = 1;
}
value &= sign - 1;
} else {
neg = 1;
}
} else {
value &= sign | (sign - 1);
}
if (ntoa2(out, arg, value, negative, log2base, precision, width, flags,
alphabet) == -1) {
return -1;
}
return 0;
return ntoa2(out, arg, value, neg, log2base, prec, width, flags, lang);
}