-
Notifications
You must be signed in to change notification settings - Fork 0
/
platgen.hs
executable file
·343 lines (276 loc) · 8.89 KB
/
platgen.hs
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#! /usr/bin/env stack
{- stack script
--resolver lts-18.28
--package text
--package raw-strings-qq
-}
-- Haskell script to generate HVM platform,
-- given list of C FFI functions and glue
{-# LANGUAGE OverloadedStrings, QuasiQuotes #-}
module PlatGen where
import System.Environment (getArgs)
import Data.Text (Text)
import qualified Data.Text as Text
import Text.RawString.QQ
main :: IO()
main = do
(PlatgenConfig sigs rawC) <- (read <$> getContents) :: IO Config
putStrLn
$ Text.unpack
$ Text.replace "////GENERATED_CID_IFNDEF_DEFINES////" (genDefines sigs)
$ Text.replace "////GENERATED_RAW_C_TO_INJECT////" (Text.unlines rawC)
$ Text.replace "////GENERATED_CASES////" (genCases sigs)
$ Text.pack template
-- Implementation details below this line
-- --------------------------------------
-- Supported types
data ArgTy
= Prim PrimTy
| Ptr String
deriving (Read,Eq)
data PrimTy
= T_uint32_t
| T_uint16_t
| T_uint
| T_size_t
| T_bool
deriving (Read,Eq)
data RetTy
= Void
| Ret ArgTy
deriving (Read,Eq)
data FunSig = CSig RetTy Text [ArgTy]
deriving (Read)
type RawFunName = Text
type CompiledName = Text
type CDefs = Text
type CExpr = Text
type CStmt = Text
data Config = PlatgenConfig
{ ffiFunSigs :: [FunSig]
, rawCToInject :: [CDefs]
}
deriving (Read)
-- Mimic HVM's name mangling (after prepending "CFun.")
-- (at time of writing, this was implemented at
-- https://github.com/Kindelia/HVM/blob/1336bb326db40d71eb09b306ddeeb8800f0c1f16/src/compiler.rs#L32)
compileName :: RawFunName -> CompiledName
compileName text = Text.toUpper $
"_" <> (Text.replace "." "_" $ Text.replace "_" "__" $ "CFun." <> text) <> "_"
printType :: PrimTy -> Text
printType T_uint = "uint"
printType T_bool = "bool"
printType T_uint32_t = "uint32_t"
printType T_uint16_t = "uint16_t"
printType T_size_t = "size_t"
getCompiledName :: FunSig -> CompiledName
getCompiledName (CSig _ rName _) = compileName rName
-- Generate a list of (re) #define s for CIDs
genDefines :: [FunSig] -> CDefs
genDefines = Text.unlines . map go . zip [0..] . map getCompiledName where
go (i,compiledName) = Text.unlines $ map Text.unwords $
[ ["#ifndef",compiledName]
, ["#define",compiledName,"(GENERATED_CID_START + ", (Text.pack $ show i),")"]
, ["#endif"]
]
-- Generate marshalling code
-- HVM -> C
genDecode :: ArgTy -> CExpr -> CExpr
genDecode (Prim pt) txt = Text.unwords ["decode_" <> printType pt,"(",txt,")"]
genDecode (Ptr str) txt = Text.unwords ["(",Text.pack str,") (decode_ptr(",txt,"))"]
-- C -> HVM
genEncode :: RetTy -> CExpr -> CExpr
genEncode (Ret (Prim pt)) txt = Text.unwords ["encode_" <> printType pt,"(",txt,")"]
genEncode Void txt = Text.unwords [txt,", encode_void()"]
genEncode (Ret (Ptr _)) txt = Text.unwords ["encode_ptr((void*)(",txt,"))"]
-- Generate arg-eval/decoding code
genDecodeArgs :: [ArgTy] -> [CStmt]
genDecodeArgs = map go . zip [0..] where
go (i,argTy) =
genDecode argTy ("whnf(wp, args_p +"<>(Text.pack $ show i)<>")")
-- Generate case-handling code
genCases :: [FunSig] -> CStmt
genCases = Text.unlines . map go where
go (CSig retTy rName args) = Text.unlines . map Text.unwords $
[ ["case",compileName rName,":{"]
, [ "ret_term = ("
, genEncode retTy $
rName <> "(\n" <> Text.intercalate ",\n" (genDecodeArgs args) <> "\n)"
, ");"
]
, ["clear(wp,args_p,",(Text.pack $ show $ length args),");"]
, ["break;}"]
]
template :: String
template = [r|
// HVM Platform template
// /////////////////////
// Platform-independent helpers
#include <stdbool.h>
#include <limits.h>
#ifndef _FFI_CALL_
#define _FFI_CALL_ (ID_TO_NAME_SIZE +0)
#endif
#ifndef _FFI_TRUE_
#define _FFI_TRUE_ (ID_TO_NAME_SIZE +1)
#endif
#ifndef _FFI_FALSE_
#define _FFI_FALSE_ (ID_TO_NAME_SIZE +2)
#endif
// (we don't #define _MAIN_, since we want compilation to fail if (Main) is missing)
#define GENERATED_CID_START (ID_TO_NAME_SIZE +3)
// Generated default CIDs for FFI functions
////GENERATED_CID_IFNDEF_DEFINES////
// Platfrom-specific raw C code
////GENERATED_RAW_C_TO_INJECT////
// Platform-independent marshalling functions
// HVM -> C
bool decode_bool(Ptr cell){
assert(get_tag(cell) == CTR);
assert(get_ext(cell) == _FFI_TRUE_ | get_ext(cell) == _FFI_FALSE_);
if (get_ext(cell) == _FFI_TRUE_) {return true;}
if (get_ext(cell) == _FFI_FALSE_) {return false;}
assert (false);
return 0;
}
uint32_t decode_uint32_t(Ptr cell) {
assert(get_tag(cell) == NUM);
assert(get_num(cell) <= UINT32_MAX);
return get_num(cell);
}
uint16_t decode_uint16_t(Ptr cell) {
assert(get_tag(cell) == NUM);
assert(get_num(cell) <= UINT16_MAX);
return get_num(cell);
}
unsigned int decode_uint(Ptr cell) {
assert(get_tag(cell) == NUM);
assert(get_num(cell) <= UINT_MAX);
return get_num(cell);
}
size_t decode_size_t(Ptr cell) {
assert(get_tag(cell) == NUM);
assert(get_num(cell) <= SIZE_MAX);
return get_num(cell);
}
void* decode_ptr(Ptr cell){
assert(get_tag(cell) == NUM);
return (void*)((size_t) get_num(cell));
}
// C -> HVM
Ptr encode_void(){
return Num(0);
}
Ptr encode_bool(bool value){
if (value) {
return Ctr(0,_FFI_TRUE_,0);
} else {
return Ctr(0,_FFI_FALSE_,0);
}
}
Ptr encode_uint32_t(uint32_t value) {
return Num((u64)value);
}
Ptr encode_uint16_t(uint16_t value) {
return Num((u64)value);
}
Ptr encode_uint(unsigned int value) {
assert(sizeof(value) <= 7);
return Num((u64)value);
}
Ptr encode_size_t(size_t value) {
assert(sizeof(value) <= 7);
return Num((u64)value);
}
Ptr encode_ptr(void* ptr){
assert (sizeof(ptr) <= 7); // pointers must fit in 60 bits (= 7.5 bytes)
return Num((u64)(size_t)ptr);
}
void dump(Worker* wp);
// Platform-independent debug helpers
#ifndef DEBUG_PRINTF
#define DEBUG_PRINTF(...) (0)
#endif
static char* str_unknown = "???";
char * decode_cid(u64 cid){
if (cid < ID_TO_NAME_SIZE) {
return id_to_name_data [cid];
} else {
return str_unknown;
}
}
void debug_print_cell(Ptr x) {
u64 tag = get_tag(x);
u64 ext = get_ext(x);
u64 val = get_val(x);
u64 num = get_num(x);
switch (tag) {
case DP0: DEBUG_PRINTF("[DP0 | %-22"PRIu64" | 0x%-20"PRIX64" ]\n", ext, val ); break;
case DP1: DEBUG_PRINTF("[DP1 | %-22"PRIu64" | 0x%-20"PRIX64" ]\n", ext, val ); break;
case VAR: DEBUG_PRINTF("[VAR | %22s | 0x%-20"PRIX64" ]\n", "", val ); break;
case ARG: DEBUG_PRINTF("[ARG | %22s | 0x%-20"PRIX64" ]\n", "", val ); break;
case ERA: DEBUG_PRINTF("[ERA | %22s | %22s ]\n", "", "" ); break;
case LAM: DEBUG_PRINTF("[LAM | %22s | 0x%-20"PRIX64" ]\n", "", val ); break;
case APP: DEBUG_PRINTF("[APP | %22s | 0x%-20"PRIX64" ]\n", "", val ); break;
case SUP: DEBUG_PRINTF("[SUP | %-22"PRIu64" | 0x%-20"PRIX64" ]\n", ext, val ); break;
case CTR: DEBUG_PRINTF("[CTR | %22s | 0x%-20"PRIX64" ]\n", decode_cid(ext), val ); break;
case FUN: DEBUG_PRINTF("[FUN | %22s | 0x%-20"PRIX64" ]\n", decode_cid(ext), val ); break;
case OP2: DEBUG_PRINTF("[OP2 | 0x%-20"PRIu64" | 0x%-20"PRIX64" ]\n", ext, val ); break;
case NUM: DEBUG_PRINTF("[NUM | %47"PRIu64" ]\n", num ); break;
default: DEBUG_PRINTF("[ ????????? 0x%-16"PRIX64" ????????? ]\n", x); break;
}
}
void dump(Worker* wp){
for (u64 i = 0; i < wp->size; i ++){
DEBUG_PRINTF("0x%-5"PRIX64"",i);
debug_print_cell(wp->node[i]);
}
}
// Main loop
int main() {
Worker hvm_mem;
Worker* wp = &hvm_mem;
build_main_term_with_args(wp,_MAIN_,0,(char**)0);
while(true){
// Evaluate main term
Ptr main_term = whnf(wp,0);
// Attempt to decode main term as (FFI.call func cont)
assert(get_tag(main_term) == CTR);
assert(get_ext(main_term) == _FFI_CALL_);
u64 func_p = get_loc(main_term,0);
u64 cont_p = get_loc(main_term,1);
// Evaluate func
Ptr func = whnf(wp, func_p);
// Generated glue code attempts to:
// 1. Decode func as (<CTR> arg1 arg2 arg3...)
// where <CTR> is one of the FFI functions (platform specific)
// 2. Evalaute args
// 3. Marshall args HVM -> C
// 4. Call FFI function
// 5. Marshall return value C -> HVM
// 6. Free args
assert(get_tag(func) == CTR);
u64 args_p = get_loc(func,0);
Ptr ret_term;
switch (get_ext(func)) {
// Generated FFI glue
////GENERATED_CASES////
default: {
u64 cid = get_ext(func);
DEBUG_PRINTF("Illegal FFI call! cid=%"PRIu64",decoded=%s\n",cid,decode_cid(cid));
dump(wp);
assert(false);
return 1;
}
}
// Cleanup: free old main term's ctr-data node: [func][cont]
clear(wp, func_p, 2);
Ptr cont = ask_lnk(wp, cont_p);
// replace main term with (cont ret_term)
u64 app_data_p = alloc(wp, 2);
link(wp, 0, App(app_data_p));
link(wp, app_data_p + 0, cont);
link(wp, app_data_p + 1, ret_term);
}
}
|]