Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epidemian
GitHub Repository: epidemian/gravity
Path: blob/master/src/compiler/gravity_ircode.h
1214 views
1
//
2
// gravity_ircode.h
3
// gravity
4
//
5
// Created by Marco Bambini on 06/11/14.
6
// Copyright (c) 2014 CreoLabs. All rights reserved.
7
//
8
9
#ifndef __GRAVITY_IRCODE__
10
#define __GRAVITY_IRCODE__
11
12
// References:
13
// https://www.usenix.org/legacy/events/vee05/full_papers/p153-yunhe.pdf
14
// http://www.lua.org/doc/jucs05.pdf
15
//
16
// In a stack-based VM, a local variable is accessed using an index, and the operand stack is accessed via the stack pointer.
17
// In a register-based VM both the local variables and operand stack can be considered as virtual registers for the method.
18
// There is a simple mapping from stack locations to register numbers, because the height and contents of the VM operand stack
19
// are known at any point in a program.
20
//
21
// All values on the operand stack can be considered as temporary variables (registers) for a method and therefore are short-lived.
22
// Their scope of life is between the instructions that push them onto the operand stack and the instruction that consumes
23
// the value on the operand stack. On the other hand, local variables (also registers) are long-lived and their life scope is
24
// the time of method execution.
25
26
#include "debug_macros.h"
27
#include "gravity_opcodes.h"
28
#include "gravity_array.h"
29
30
#define IRCODE_LATEST UINT32_MAX
31
32
typedef enum {
33
NO_TAG = 0,
34
INT_TAG,
35
DOUBLE_TAG,
36
LABEL_TAG,
37
SKIP_TAG,
38
RANGE_INCLUDE_TAG,
39
RANGE_EXCLUDE_TAG,
40
PRAGMA_OPTIMIZATION
41
} optag_t;
42
43
typedef struct {
44
opcode_t op;
45
optag_t tag;
46
int32_t p1;
47
int32_t p2;
48
int32_t p3;
49
union {
50
double d; // tag is DOUBLE_TAG
51
int64_t n; // tag is INT_TAG
52
};
53
} inst_t;
54
55
typedef struct ircode_t ircode_t;
56
57
ircode_t *ircode_create (uint16_t nlocals);
58
void ircode_free (ircode_t *code);
59
uint32_t ircode_count (ircode_t *code);
60
uint32_t ircode_ntemps (ircode_t *code);
61
inst_t *ircode_get (ircode_t *code, uint32_t index);
62
void ircode_dump (void *code);
63
void ircode_push_context (ircode_t *code);
64
void ircode_pop_context (ircode_t *code);
65
bool ircode_iserror (ircode_t *code);
66
void ircode_patch_init (ircode_t *code, uint16_t index);
67
68
uint32_t ircode_newlabel (ircode_t *code);
69
void ircode_setlabel_true (ircode_t *code, uint32_t nlabel);
70
void ircode_setlabel_false (ircode_t *code, uint32_t nlabel);
71
void ircode_unsetlabel_true (ircode_t *code);
72
void ircode_unsetlabel_false (ircode_t *code);
73
uint32_t ircode_getlabel_true (ircode_t *code);
74
uint32_t ircode_getlabel_false (ircode_t *code);
75
void ircode_marklabel (ircode_t *code, uint32_t nlabel);
76
77
void inst_setskip (inst_t *inst);
78
uint8_t opcode_numop (opcode_t op);
79
80
void ircode_add (ircode_t *code, opcode_t op, uint32_t p1, uint32_t p2, uint32_t p3);
81
void ircode_add_tag (ircode_t *code, opcode_t op, uint32_t p1, uint32_t p2, uint32_t p3, optag_t tag);
82
void ircode_add_array (ircode_t *code, opcode_t op, uint32_t p1, uint32_t p2, uint32_t p3, uint32_r r);
83
void ircode_add_double (ircode_t *code, double d);
84
void ircode_add_int (ircode_t *code, int64_t n);
85
void ircode_add_constant (ircode_t *code, uint32_t index);
86
void ircode_add_skip (ircode_t *code);
87
void ircode_set_index (uint32_t index, ircode_t *code, opcode_t op, uint32_t p1, uint32_t p2, uint32_t p3);
88
void ircode_setarray_index (uint32_t index, ircode_t *code, opcode_t op, uint32_t p1, uint32_t p2, uint32_t p3, uint32_r r);
89
90
bool ircode_register_istemp (ircode_t *code, uint32_t n);
91
uint32_t ircode_register_push_temp (ircode_t *code);
92
uint32_t ircode_register_push (ircode_t *code, uint32_t nreg);
93
uint32_t ircode_register_pop (ircode_t *code);
94
uint32_t ircode_register_pop_protect (ircode_t *code, bool protect);
95
void ircode_register_protect (ircode_t *code, uint32_t nreg);
96
uint32_t ircode_register_last (ircode_t *code);
97
uint32_t ircode_register_count (ircode_t *code);
98
void ircode_register_clean (ircode_t *code, uint32_t nreg);
99
void ircode_register_dump (ircode_t *code);
100
101
#endif
102
103