truthlinked-axiom-sdk 0.1.3

TruthLinked Axiom Cell SDK — IR pipeline, regalloc, codegen, CellBuilder
Documentation
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
//! CellBuilder - fluent bytecode assembler for Axiom cells.

use truthlinked_axiom::bytecode::CellBytecode;
use truthlinked_axiom::opcode::tag;

struct PendingJump {
    patch_offset: usize,
    label: String,
}

pub struct CellBuilder {
    code: Vec<u8>,
    const_pool: Vec<Vec<u8>>,
    labels: std::collections::HashMap<String, u32>,
    pending: Vec<PendingJump>,
}

impl CellBuilder {
    pub fn new() -> Self {
        Self {
            code: Vec::new(),
            const_pool: Vec::new(),
            labels: std::collections::HashMap::new(),
            pending: Vec::new(),
        }
    }

    pub fn add_const(&mut self, data: Vec<u8>) -> u16 {
        let idx = self.const_pool.len() as u16;
        self.const_pool.push(data);
        idx
    }

    pub fn label(&mut self, name: &str) -> &mut Self {
        self.labels.insert(name.to_string(), self.code.len() as u32);
        self
    }

    pub fn offset(&self) -> u32 {
        self.code.len() as u32
    }

    fn e1(&mut self, op: u8) {
        self.code.push(op);
    }
    fn e2(&mut self, op: u8, a: u8) {
        self.code.push(op);
        self.code.push(a);
    }
    fn e3(&mut self, op: u8, a: u8, b: u8) {
        self.code.push(op);
        self.code.push(a);
        self.code.push(b);
    }
    fn e4(&mut self, op: u8, a: u8, b: u8, c: u8) {
        self.code.push(op);
        self.code.push(a);
        self.code.push(b);
        self.code.push(c);
    }
    fn eu16(&mut self, v: u16) {
        self.code.extend_from_slice(&v.to_le_bytes());
    }
    fn eu32(&mut self, v: u32) {
        self.code.extend_from_slice(&v.to_le_bytes());
    }
    fn eu64(&mut self, v: u64) {
        self.code.extend_from_slice(&v.to_le_bytes());
    }

    fn jump_pending(&mut self, op: u8, cond: Option<u8>, label: &str) -> &mut Self {
        self.code.push(op);
        if let Some(c) = cond {
            self.code.push(c);
        }
        let patch = self.code.len();
        self.eu32(0);
        self.pending.push(PendingJump {
            patch_offset: patch,
            label: label.to_string(),
        });
        self
    }

    // Arithmetic
    pub fn add(&mut self, d: u8, a: u8, b: u8) -> &mut Self {
        self.e4(tag::ADD, d, a, b);
        self
    }
    pub fn sub(&mut self, d: u8, a: u8, b: u8) -> &mut Self {
        self.e4(tag::SUB, d, a, b);
        self
    }
    pub fn mul(&mut self, d: u8, a: u8, b: u8) -> &mut Self {
        self.e4(tag::MUL, d, a, b);
        self
    }
    pub fn div(&mut self, d: u8, a: u8, b: u8) -> &mut Self {
        self.e4(tag::DIV, d, a, b);
        self
    }
    pub fn modulo(&mut self, d: u8, a: u8, b: u8) -> &mut Self {
        self.e4(tag::MOD, d, a, b);
        self
    }
    pub fn add_sat(&mut self, d: u8, a: u8, b: u8) -> &mut Self {
        self.e4(tag::ADD_SAT, d, a, b);
        self
    }
    pub fn sub_sat(&mut self, d: u8, a: u8, b: u8) -> &mut Self {
        self.e4(tag::SUB_SAT, d, a, b);
        self
    }
    // Bitwise
    pub fn and(&mut self, d: u8, a: u8, b: u8) -> &mut Self {
        self.e4(tag::AND, d, a, b);
        self
    }
    pub fn or(&mut self, d: u8, a: u8, b: u8) -> &mut Self {
        self.e4(tag::OR, d, a, b);
        self
    }
    pub fn xor(&mut self, d: u8, a: u8, b: u8) -> &mut Self {
        self.e4(tag::XOR, d, a, b);
        self
    }
    pub fn not(&mut self, d: u8, a: u8) -> &mut Self {
        self.e3(tag::NOT, d, a);
        self
    }
    pub fn shl(&mut self, d: u8, a: u8, s: u8) -> &mut Self {
        self.e4(tag::SHL, d, a, s);
        self
    }
    pub fn shr(&mut self, d: u8, a: u8, s: u8) -> &mut Self {
        self.e4(tag::SHR, d, a, s);
        self
    }
    // Comparison
    pub fn eq(&mut self, d: u8, a: u8, b: u8) -> &mut Self {
        self.e4(tag::EQ, d, a, b);
        self
    }
    pub fn ne(&mut self, d: u8, a: u8, b: u8) -> &mut Self {
        self.e4(tag::NE, d, a, b);
        self
    }
    pub fn lt(&mut self, d: u8, a: u8, b: u8) -> &mut Self {
        self.e4(tag::LT, d, a, b);
        self
    }
    pub fn lte(&mut self, d: u8, a: u8, b: u8) -> &mut Self {
        self.e4(tag::LTE, d, a, b);
        self
    }
    pub fn gt(&mut self, d: u8, a: u8, b: u8) -> &mut Self {
        self.e4(tag::GT, d, a, b);
        self
    }
    pub fn gte(&mut self, d: u8, a: u8, b: u8) -> &mut Self {
        self.e4(tag::GTE, d, a, b);
        self
    }
    pub fn is_zero(&mut self, d: u8, a: u8) -> &mut Self {
        self.e3(tag::IS_ZERO, d, a);
        self
    }
    // Control flow
    pub fn jump(&mut self, t: u32) -> &mut Self {
        self.e1(tag::JUMP);
        self.eu32(t);
        self
    }
    pub fn jump_if(&mut self, c: u8, t: u32) -> &mut Self {
        self.e2(tag::JUMP_IF, c);
        self.eu32(t);
        self
    }
    pub fn jump_if_not(&mut self, c: u8, t: u32) -> &mut Self {
        self.e2(tag::JUMP_IF_NOT, c);
        self.eu32(t);
        self
    }
    pub fn jump_label(&mut self, l: &str) -> &mut Self {
        self.jump_pending(tag::JUMP, None, l)
    }
    pub fn jump_if_label(&mut self, c: u8, l: &str) -> &mut Self {
        self.jump_pending(tag::JUMP_IF, Some(c), l)
    }
    pub fn jump_if_not_label(&mut self, c: u8, l: &str) -> &mut Self {
        self.jump_pending(tag::JUMP_IF_NOT, Some(c), l)
    }
    pub fn call(&mut self, t: u32) -> &mut Self {
        self.e1(tag::CALL);
        self.eu32(t);
        self
    }
    pub fn call_label(&mut self, l: &str) -> &mut Self {
        self.jump_pending(tag::CALL, None, l)
    }
    pub fn ret(&mut self) -> &mut Self {
        self.e1(tag::RETURN);
        self
    }
    pub fn halt(&mut self) -> &mut Self {
        self.e1(tag::HALT);
        self
    }
    pub fn trap(&mut self, c: u16) -> &mut Self {
        self.e1(tag::TRAP);
        self.eu16(c);
        self
    }
    // Data
    pub fn load_const(&mut self, d: u8, i: u16) -> &mut Self {
        self.e2(tag::LOAD_CONST, d);
        self.eu16(i);
        self
    }
    pub fn load_imm8(&mut self, d: u8, v: u8) -> &mut Self {
        self.e3(tag::LOAD_IMM8, d, v);
        self
    }
    pub fn load_imm64(&mut self, d: u8, v: u64) -> &mut Self {
        self.e2(tag::LOAD_IMM64, d);
        self.eu64(v);
        self
    }
    pub fn load_bytes32(&mut self, d: u8, data: [u8; 32]) -> &mut Self {
        let i = self.add_const(data.to_vec());
        self.load_const(d, i)
    }
    pub fn mov(&mut self, d: u8, s: u8) -> &mut Self {
        self.e3(tag::MOVE, d, s);
        self
    }
    pub fn swap(&mut self, a: u8, b: u8) -> &mut Self {
        self.e3(tag::SWAP, a, b);
        self
    }
    // Storage
    pub fn sload(&mut self, d: u8, k: u8) -> &mut Self {
        self.e3(tag::SLOAD, d, k);
        self
    }
    pub fn sstore(&mut self, k: u8, v: u8) -> &mut Self {
        self.e3(tag::SSTORE, k, v);
        self
    }
    pub fn sdelete(&mut self, k: u8) -> &mut Self {
        self.e2(tag::SDELETE, k);
        self
    }
    // Context
    pub fn get_caller(&mut self, d: u8) -> &mut Self {
        self.e2(tag::GET_CALLER, d);
        self
    }
    pub fn get_owner(&mut self, d: u8) -> &mut Self {
        self.e2(tag::GET_OWNER, d);
        self
    }
    pub fn get_cell_id(&mut self, d: u8) -> &mut Self {
        self.e2(tag::GET_CELL_ID, d);
        self
    }
    pub fn get_height(&mut self, d: u8) -> &mut Self {
        self.e2(tag::GET_HEIGHT, d);
        self
    }
    pub fn get_timestamp(&mut self, d: u8) -> &mut Self {
        self.e2(tag::GET_TIMESTAMP, d);
        self
    }
    pub fn get_value(&mut self, d: u8) -> &mut Self {
        self.e2(tag::GET_VALUE, d);
        self
    }
    pub fn get_calldata_len(&mut self, d: u8) -> &mut Self {
        self.e2(tag::GET_CALLDATA_LEN, d);
        self
    }
    pub fn get_calldata(&mut self, d: u8, off: u8) -> &mut Self {
        self.e3(tag::GET_CALLDATA, d, off);
        self
    }
    // Output
    pub fn set_return(&mut self, i: u8, l: u8) -> &mut Self {
        self.e3(tag::SET_RETURN, i, l);
        self
    }
    pub fn set_return_reg(&mut self, d: u8, l: u8) -> &mut Self {
        self.e3(tag::SET_RETURN_REG, d, l);
        self
    }
    pub fn emit_log(&mut self, t: u8, d: u8) -> &mut Self {
        self.e3(tag::EMIT_LOG, t, d);
        self
    }
    pub fn emit_log_reg(&mut self, t: u8, d: u8, l: u8) -> &mut Self {
        self.e4(tag::EMIT_LOG_REG, t, d, l);
        self
    }
    // Cross-cell
    pub fn call_cell(&mut self, cell: u8, cd: u8, len: u8, val: u8) -> &mut Self {
        self.e1(tag::CALL_CELL);
        self.code.extend_from_slice(&[cell, cd, len, val]);
        self
    }
    // Call buffer
    pub fn buf_reset(&mut self) -> &mut Self {
        self.e1(tag::BUF_RESET);
        self
    }
    pub fn buf_write_const(&mut self, idx: u16) -> &mut Self {
        self.e1(tag::BUF_WRITE_CONST);
        self.eu16(idx);
        self
    }
    pub fn buf_write_reg(&mut self, src: u8) -> &mut Self {
        self.e2(tag::BUF_WRITE_REG, src);
        self
    }
    pub fn buf_call_cell(&mut self, cell: u8, val: u8) -> &mut Self {
        self.e3(tag::BUF_CALL_CELL, cell, val);
        self
    }
    pub fn buf_set_return(&mut self) -> &mut Self {
        self.e1(tag::BUF_SET_RETURN);
        self
    }
    // Crypto
    pub fn hash32(&mut self, d: u8, s: u8) -> &mut Self {
        self.e3(tag::HASH32, d, s);
        self
    }
    pub fn hash32_const(&mut self, d: u8, i: u16) -> &mut Self {
        self.e2(tag::HASH32_CONST, d);
        self.eu16(i);
        self
    }
    // Access control
    pub fn require_owner(&mut self) -> &mut Self {
        self.e1(tag::REQUIRE_OWNER);
        self
    }
    pub fn require_caller(&mut self, r: u8) -> &mut Self {
        self.e2(tag::REQUIRE_CALLER, r);
        self
    }
    pub fn require_eq(&mut self, a: u8, b: u8) -> &mut Self {
        self.e3(tag::REQUIRE_EQ, a, b);
        self
    }
    pub fn require_ne(&mut self, a: u8, b: u8) -> &mut Self {
        self.e3(tag::REQUIRE_NE, a, b);
        self
    }
    pub fn require_lt(&mut self, a: u8, b: u8) -> &mut Self {
        self.e3(tag::REQUIRE_LT, a, b);
        self
    }
    pub fn require_non_zero(&mut self, r: u8) -> &mut Self {
        self.e2(tag::REQUIRE_NON_ZERO, r);
        self
    }
    pub fn require_gas(&mut self, t: u64) -> &mut Self {
        self.e1(tag::REQUIRE_GAS);
        self.eu64(t);
        self
    }
    // Token
    pub fn token_balance(&mut self, d: u8, tok: u8, acc: u8) -> &mut Self {
        self.e4(tag::TOKEN_BALANCE, d, tok, acc);
        self
    }
    pub fn token_transfer(&mut self, tok: u8, from: u8, to: u8, amt: u8) -> &mut Self {
        self.e1(tag::TOKEN_TRANSFER);
        self.code.extend_from_slice(&[tok, from, to, amt]);
        self
    }
    pub fn token_mint(&mut self, tok: u8, rec: u8, amt: u8) -> &mut Self {
        self.e4(tag::TOKEN_MINT, tok, rec, amt);
        self
    }
    pub fn token_burn(&mut self, tok: u8, own: u8, amt: u8) -> &mut Self {
        self.e4(tag::TOKEN_BURN, tok, own, amt);
        self
    }
    pub fn token_freeze(&mut self, tok: u8, acc: u8) -> &mut Self {
        self.e3(tag::TOKEN_FREEZE, tok, acc);
        self
    }
    pub fn token_thaw(&mut self, tok: u8, acc: u8) -> &mut Self {
        self.e3(tag::TOKEN_THAW, tok, acc);
        self
    }
    // Accord
    pub fn accord_request(&mut self, dst: u8, url_r: u8, meth_r: u8, body_r: u8) -> &mut Self {
        self.e1(tag::ACCORD_REQUEST);
        self.code.extend_from_slice(&[dst, url_r, meth_r, body_r]);
        self
    }
    pub fn accord_read(&mut self, dst: u8, req_r: u8) -> &mut Self {
        self.e3(tag::ACCORD_READ, dst, req_r);
        self
    }

    /// Resolve labels and encode to Axiom bytecode bytes.
    pub fn build(&mut self) -> Vec<u8> {
        for jump in self.pending.drain(..) {
            let target = *self
                .labels
                .get(&jump.label)
                .unwrap_or_else(|| panic!("undefined label: {}", jump.label));
            self.code[jump.patch_offset..jump.patch_offset + 4]
                .copy_from_slice(&target.to_le_bytes());
        }
        CellBytecode {
            const_pool: self.const_pool.clone(),
            code: self.code.clone(),
        }
        .encode()
    }
}

impl Default for CellBuilder {
    fn default() -> Self {
        Self::new()
    }
}