potion  0.2
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
asm.c
Go to the documentation of this file.
1 
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <math.h>
8 #include "potion.h"
9 #include "internal.h"
10 #include "opcodes.h"
11 #include "asm.h"
12 
14  int siz = ASM_UNIT - sizeof(PNAsm);
15  PNAsm * volatile asmb = PN_FLEX_NEW(asmb, PN_TBYTES, PNAsm, siz);
16  return asmb;
17 }
18 
19 PNAsm *potion_asm_clear(Potion *P, PNAsm * volatile asmb) {
20  asmb->len = 0;
21  PN_MEMZERO_N(asmb->ptr, u8, asmb->siz);
22  return asmb;
23 }
24 
25 PNAsm *potion_asm_put(Potion *P, PNAsm * volatile asmb, PN val, size_t len) {
26  u8 *ptr;
27  PN_FLEX_NEEDS(len, asmb, PN_TBYTES, PNAsm, ASM_UNIT);
28  ptr = asmb->ptr + asmb->len;
29 
30  if (len == sizeof(u8))
31  *ptr = (u8)val;
32  else if (len == sizeof(int))
33  *((int *)ptr) = (int)val;
34  else if (len == sizeof(PN))
35  *((PN *)ptr) = val;
36  else if (len == 2)
37  *((short *)ptr) = (short)val;
38 
39  asmb->len += len;
40  return asmb;
41 }
42 
43 PNAsm *potion_asm_op(Potion *P, PNAsm * volatile asmb, u8 ins, int _a, int _b) {
44  PN_OP *pos;
45  PN_FLEX_NEEDS(sizeof(PN_OP), asmb, PN_TBYTES, PNAsm, ASM_UNIT);
46  pos = (PN_OP *)(asmb->ptr + asmb->len);
47 
48  pos->code = ins;
49  pos->a = _a;
50  pos->b = _b;
51 
52  asmb->len += sizeof(PN_OP);
53  return asmb;
54 }
55 
56 PNAsm *potion_asm_write(Potion *P, PNAsm * volatile asmb, char *str, size_t len) {
57  char *ptr;
58  PN_FLEX_NEEDS(len, asmb, PN_TBYTES, PNAsm, ASM_UNIT);
59  ptr = (char *)asmb->ptr + asmb->len;
60  PN_MEMCPY_N(ptr, str, char, len);
61  asmb->len += len;
62  return asmb;
63 }
#define PN_FLEX_NEW(N, V, T, S)
Definition: internal.h:35
#define PN_FLEX_NEEDS(X, N, V, T, S)
Definition: internal.h:40
PNAsm * potion_asm_write(Potion *P, PNAsm *volatile asmb, char *str, size_t len)
Definition: asm.c:56
#define PN_MEMZERO_N(X, T, N)
Definition: internal.h:20
PNAsm * potion_asm_clear(Potion *P, PNAsm *volatile asmb)
Definition: asm.c:19
#define ASM_UNIT
Definition: asm.h:16
Definition: potion.h:577
the Potion VM instruction set (heavily based on Lua's)
#define PN_TBYTES
Definition: potion.h:121
PNAsm * potion_asm_put(Potion *P, PNAsm *volatile asmb, PN val, size_t len)
Definition: asm.c:25
#define PN_OP(T, A, B)
Warning: This might conflict with the typedef struct PN_OP.
Definition: ast.h:28
PN_SIZE siz
Definition: potion.h:577
PNAsm * potion_asm_op(Potion *P, PNAsm *volatile asmb, u8 ins, int _a, int _b)
Definition: asm.c:43
unsigned char u8
Definition: internal.h:8
#define PN_MEMCPY_N(X, Y, T, N)
Definition: internal.h:22
some assembler macros
PNAsm * potion_asm_new(Potion *P)
Definition: asm.c:13
non-API internal parts
PN_SIZE len
Definition: potion.h:577
int a
< the op. See vm.c http://www.lua.org/doc/jucs05.pdf
Definition: opcodes.h:71
the global interpreter state P. currently singleton (not threads yet)
Definition: potion.h:644
The potion API.
unsigned char ptr[]
Definition: potion.h:577
volatile _PN PN
Definition: potion.h:81
int b
optional arg, the message
Definition: opcodes.h:73
PN_OP - a compressed three-address op (as 32bit int bitfield) TODO: expand to 64bit, check jit then.
Definition: opcodes.h:70