-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathw3.h
186 lines (151 loc) · 5.13 KB
/
w3.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// A WebAssembly codebase by Jay Krell
//
// https://webassembly.github.io/spec/core/binary/index.html
// https://webassembly.github.io/spec/core/_download/WebAssembly.pdf
#pragma once
#include "w3-1.h"
typedef ptrdiff_t ssize_t;
#include <assert.h>
#include <errno.h>
#include <memory.h>
#include <stack>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stddef.h>
#include <string>
#include <vector>
#include "w3Tag.h"
#include "w3Label.h"
std::string StringFormat (PCSTR format, ...);
void ThrowString (const std::string& a);
#define _DARWIN_USE_64_BIT_INODE 1
//#define __DARWIN_ONLY_64_BIT_INO_T 1
// TODO cmake
// TODO big endian and packed I/O
//#define _LARGEFILE_SOURCE
//#define _LARGEFILE64_SOURCE
#if _MSC_VER
#include <intrin.h>
#endif
#if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic ignored "-Wunused-const-variable"
#pragma GCC diagnostic ignored "-Wunused-function"
#endif
#ifndef _ISOC99_SOURCE
#define _ISOC99_SOURCE
#endif
#ifdef _MSC_VER
#include <malloc.h>
#define alloca _alloca
#endif
size_t string_vformat_length (PCSTR format, va_list va);
//void AssertFailedFormat (PCSTR condition, const std::string& extra);
void AssertFailed (PCSTR expr);
#ifdef _WIN64
#define FORMAT_SIZE "I64"
#define long_t __int64 // aka ptrdiff, aka Unix long but not Windows long
#else
#define FORMAT_SIZE "l"
#define long_t long // aka ptrdiff, aka Unix long but not Windows long
#endif
struct w3Code;
struct w3DecodedInstruction;
struct explicit_operator_bool;
struct w3Fd;
struct w3Frame; // work in progress
struct w3Function;
struct w3FunctionType;
struct w3Handle;
struct w3Label;
struct w3MemoryMappedFile;
struct w3Module;
struct w3ModuleInstance;
struct w3Runtime;
struct w3Section;
struct w3Stack;
struct w3StackValue;
struct w3stream;
struct w3Variable; //local, global, temp
char TagChar(w3Tag t); //todo: short string?
uint32_t GetUint16LE (const void* a);
uint32_t GetUint32LE (const void* a);
uint64_t SignExtend (uint64_t value, uint32_t bits);
size_t ssize_magnitude (ssize_t i);
size_t int_magnitude (ssize_t i); //todo remove
uint32_t UIntGetPrecision (uint64_t a); // How many bits needed to represent.
uint32_t IntGetPrecision (int64_t a); // How many bits needed to represent.
uint32_t UIntToDec_GetLength (uint64_t b); // e.g. 99=>2
uint32_t IntToDec_GetLength (int64_t a); // e.g. -99=>3
uint32_t UIntToHex_GetLength (uint64_t b); // e.g. 255=>2
// If negative and first digit is <8, add one to induce leading 8-F
// so that sign extension of most significant bit will work.
// This might be a bad idea. TODO.
uint32_t IntToHex_GetLength (int64_t a);
uint32_t UIntToDec (uint64_t a, PCH buf); // returns length
uint32_t IntToDec (int64_t a, PCH buf); // returns length
void UIntToHexLength (uint64_t a, uint32_t len, PCH buf);
void IntToHexLength (int64_t a, uint32_t len, PCH buf);
uint32_t IntToHex (int64_t a, PCH buf); // returns length
uint32_t IntToHex8 (int64_t a, PCH buf); // always outputs 8 chars and returns 8
uint32_t IntToHex_AtLeast8 (int64_t a, PCH buf); // always outputs at least 8 chars
uint32_t UIntToHex_AtLeast8 (uint64_t a, PCH buf); // always outputs at least 8 chars
uint32_t UIntToHex_GetLength_AtLeast8 (uint64_t a); // always at least 8
//todo these all need module or thread or other context,
// like for error reporting maybe, and move cursor there possibly..
uint8_t read_byte (uint8_t** cursor, const uint8_t* end);
uint64_t read_varuint64 (uint8_t** cursor, const uint8_t* end);
uint32_t read_varuint32 (uint8_t** cursor, const uint8_t* end);
uint8_t read_varuint7 (uint8_t** cursor, const uint8_t* end);
int64_t read_varint64 (uint8_t** cursor, const uint8_t* end);
int32_t read_varint32 (uint8_t** cursor, const uint8_t* end);
//todo don't use C++ or at least not exceptions?
void ThrowInt (int i, PCSTR a = "");
void ThrowErrno (PCSTR a = "");
//todo commit to C++11? probably not
// C++98 workaround for what C++11 offers.
struct explicit_operator_bool
{
typedef void (explicit_operator_bool::*T) () const;
#if 1
void True () const { }
#else
void True () const;
#endif
};
typedef void (explicit_operator_bool::*bool_type) () const;
#if _WIN32
void throw_Win32Error (int err, PCSTR a);
void throw_GetLastError (PCSTR a);
#endif
void DecodeFunction (w3Module* module, w3Code* code, uint8_t** cursor);
//todo: trim these?
#ifdef _WIN32
#define NOMINMAX 1
#include <io.h>
#include <windows.h>
#define DebugBreak __debugbreak
#else
typedef char* PCH;
typedef const char* PCSTR;
#define IsDebuggerPresent() (0)
#define __debugbreak() ((void)0)
#define DebugBreak() ((void)0)
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#endif
#ifdef _MSC_VER
#pragma warning(disable:4061) // enumerator in switch of enum is not explicitly handled
#endif
PCH VarName(w3Variable* var);
void AssertFailedFormat (PCSTR condition, const std::string& extra);
void AssertFailed (PCSTR expr);
#define Assert(x) ((x) || ( AssertFailed (#x), (int)0))
#define AssertFormat(x, extra) ((x) || (AssertFailedFormat (#x, StringFormat extra), 0))
#include "w3Immediate.h"
#include "w3InstructionMacros.h"
#include "w3InstructionEnum.h"
#include "w3BitsForInt.h"