varaq-wasm-c/compiler.h

260 lines
4.7 KiB
C
Raw Normal View History

2023-02-04 14:06:00 -05:00
#ifndef COMPILER_H
#define COMPILER_H
#include <inttypes.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
typedef enum Opcodes
{
UNREACHABLE = 0,
NOP = 1,
BLOCK = 2,
LOOP = 3,
IF = 4,
ELSE = 5,
TRY = 6, // PROPOSED
CATCH = 7, // PROPOSED
THROW = 8, // PROPOSED
RETHROW = 9, // PROPOSED
BR_ON_EXN = 10, // PROPOSED
END = 11,
BR = 12,
BR_IF = 13,
BR_TABLE = 14,
RETURN = 15,
CALL = 16,
CALL_INDIRECT = 17,
RETURN_CALL = 18, // PROPOSED
RETURN_CALL_INDIRECT = 19, // PROPOSED
DROP = 26,
SELECT = 27,
SELECT_T = 28, // PROPOSED
LOCAL_GET = 32,
LOCAL_SET = 33,
LOCAL_TEE = 34,
GLOBAL_GET = 35,
GLOBAL_SET = 36,
TABLE_GET = 37, // PROPOSED
TABLE_SET = 38, // PROPOSED
I32_LOAD = 40,
I64_LOAD = 41,
F32_LOAD = 42,
F64_LOAD = 43,
I32_LOAD8_S = 44,
I32_LOAD8_U = 45,
I32_LOAD16_S = 46,
I32_LOAD16_U = 47,
I64_LOAD8_S = 48,
I64_LOAD8_U = 49,
I64_LOAD16_S = 50,
I64_LOAD16_U = 51,
I64_LOAD32_S = 52,
I64_LOAD32_U = 53,
I32_STORE = 54,
I64_STORE = 55,
F32_STORE = 56,
F64_STORE = 57,
I32_STORE8 = 58,
I32_STORE16 = 59,
I64_STORE8 = 60,
I64_STORE16 = 61,
I64_STORE32 = 62,
MEMORY_SIZE = 63,
MEMORY_GROW = 64,
I32_CONST = 65,
I64_CONST = 66,
F32_CONST = 67,
F64_CONST = 68,
I32_EQZ = 69,
I32_EQ = 70,
I32_NE = 71,
I32_LT_S = 72,
I32_LT_U = 73,
I32_GT_S = 74,
I32_GT_U = 75,
I32_LE_S = 76,
I32_LE_U = 77,
I32_GE_S = 78,
I32_GE_U = 79,
I64_EQZ = 80,
I64_EQ = 81,
I64_NE = 82,
I64_LT_S = 83,
I64_LT_U = 84,
I64_GT_S = 85,
I64_GT_U = 86,
I64_LE_S = 87,
I64_LE_U = 88,
I64_GE_S = 89,
I64_GE_U = 90,
F32_EQ = 91,
F32_NE = 92,
F32_LT = 93,
F32_GT = 94,
F32_LE = 95,
F32_GE = 96,
F64_EQ = 97,
F64_NE = 98,
F64_LT = 99,
F64_GT = 100,
F64_LE = 101,
F64_GE = 102,
I32_CLZ = 103,
I32_CTZ = 104,
I32_POPCNT = 105,
I32_ADD = 106,
I32_SUB = 107,
I32_MUL = 108,
I32_DIV_S = 109,
I32_DIV_U = 110,
I32_REM_S = 111,
I32_REM_U = 112,
I32_AND = 113,
I32_OR = 114,
I32_XOR = 115,
I32_SHL = 116,
I32_SHR_S = 117,
I32_SHR_U = 118,
I32_ROTL = 119,
I32_ROTR = 120,
I64_CLZ = 121,
I64_CTZ = 122,
I64_POPCNT = 123,
I64_ADD = 124,
I64_SUB = 125,
I64_MUL = 126,
I64_DIV_S = 127,
I64_DIV_U = 128,
I64_REM_S = 129,
I64_REM_U = 130,
I64_AND = 131,
I64_OR = 132,
I64_XOR = 133,
I64_SHL = 134,
I64_SHR_S = 135,
I64_SHR_U = 136,
I64_ROTL = 137,
I64_ROTR = 138,
F32_ABS = 139,
F32_NEG = 140,
F32_CEIL = 141,
F32_FLOOR = 142,
F32_TRUNC = 143,
F32_NEAREST = 144,
F32_SQRT = 145,
F32_ADD = 146,
F32_SUB = 147,
F32_MUL = 148,
F32_DIV = 149,
F32_MIN = 150,
F32_MAX = 151,
F32_COPYSIGN = 152,
F64_ABS = 153,
F64_NEG = 154,
F64_CEIL = 155,
F64_FLOOR = 156,
F64_TRUNC = 157,
F64_NEAREST = 158,
F64_SQRT = 159,
F64_ADD = 160,
F64_SUB = 161,
F64_MUL = 162,
F64_DIV = 163,
F64_MIN = 164,
F64_MAX = 165,
F64_COPYSIGN = 166,
I32_WRAP_I64 = 167,
I32_TRUNC_F32_S = 168,
I32_TRUNC_F32_U = 169,
I32_TRUNC_F64_S = 170,
I32_TRUNC_F64_U = 171,
I64_EXTEND_I32_S = 172,
I64_EXTEND_I32_U = 173,
I64_TRUNC_F32_S = 174,
I64_TRUNC_F32_U = 175,
I64_TRUNC_F64_S = 176,
I64_TRUNC_F64_U = 177,
F32_CONVERT_I32_S = 178,
F32_CONVERT_I32_U = 179,
F32_CONVERT_I64_S = 180,
F32_CONVERT_I64_U = 181,
F32_DEMOTE_F64 = 182,
F64_CONVERT_I32_S = 183,
F64_CONVERT_I32_U = 184,
F64_CONVERT_I64_S = 185,
F64_CONVERT_I64_U = 186,
F64_PROMOTE_F32 = 187,
I32_REINTERPRET_F32 = 188,
I64_REINTERPRET_F64 = 189,
F32_REINTERPRET_I32 = 190,
F64_REINTERPRET_I64 = 191,
OP_NULL = 0xD0,
IS_NULL = 0xD1,
REF_FUNC = 0xD2,
} Opcodes;
typedef enum Types
{
i32 = 0x7F,
i64 = 0x7E,
f32 = 0x7D,
f64 = 0x7C,
v128 = 0x7B,
funcref = 0x70,
externref = 0x6F,
} Types;
typedef enum FunctionTypes
{
EMPTY_ARRAY = 0,
FUNCTION = 96
} FunctionTypes;
typedef enum ExportTypes
{
EXPORT_FUNC = 0,
EXPORT_TABLE = 1,
EXPORT_MEM = 2,
EXPORT_GLOBAL = 3
} ExportTypes;
typedef enum Section
{
CUSTOM = 0,
TYPE = 1,
IMPORT = 2,
FUNC = 3,
TABLE = 4,
MEMORY = 5,
GLOBAL = 6,
EXPORT = 7,
START = 8,
ELEMENT = 9,
CODE = 10,
DATA = 11
} Section;
typedef struct Code Code;
struct Code
{
uint8_t *cells;
size_t count;
bool override;
size_t override_count;
};
Code *signedLEB128 (size_t num);
Code *unsignedLEB128 (size_t num);
Code *append_byte (Code *tape, uint8_t data);
Code *append (Code *tape, Code *data);
Code *encodeString (char *string);
Code *encodeVector (Code *data);
Code *createSection (uint8_t section, Code *data);
Code *demo_function_compile ();
Code *demo_add_compile ();
Code *compile (char *buffer);
#endif