Add Glulxe and Git. They compile but don't work yet.
[projects/chimara/chimara.git] / interpreters / git / compiler.h
1 // Header for compiler.c
2 // $Id: compiler.h,v 1.11 2004/02/02 00:13:46 iain Exp $
3
4 #ifndef GIT_COMPILER_H
5 #define GIT_COMPILER_H
6
7 // -------------------------------------------------------------
8 // Types
9
10 typedef enum
11 {
12 #define LABEL(foo) label_ ## foo,
13 #include "labels.inc"
14     MAX_LABEL
15 }
16 Label;
17
18 #ifdef USE_DIRECT_THREADING
19 typedef void* Opcode; // Generated opcode: pointer to a label in exec().
20 #else
21 typedef Label Opcode;
22 #endif
23
24 typedef git_uint32* Block; // Generated code block: array of labels.
25
26 // -------------------------------------------------------------
27 // Settings
28
29 extern int gPeephole; // Peephole optimisation of generated code?
30 extern int gDebug;    // Insert debug statements into generated code?
31 extern int gCacheRAM; // Keep RAM-based code in the JIT cache?
32
33 // -------------------------------------------------------------
34 // Compiling code
35
36 extern const char* gLabelNames[];
37
38 extern void initCompiler (size_t cacheSize);
39 extern void shutdownCompiler ();
40
41 extern void emitData (git_uint32);
42 extern void emitFinalCode (Label);
43 extern void emitConstBranch (Label op, git_uint32 address);
44
45 extern void abortCompilation ();
46
47 extern git_uint32 undoEmit();
48 extern void nextInstructionIsReferenced ();
49
50 extern Block peekAtEmittedStuff (int numOpcodes);
51
52 // -------------------------------------------------------------
53 // Accessing compiled code
54
55 extern void pruneCodeCache (git_uint32 start, git_uint32 size);
56 extern void resetCodeCache ();
57 extern void compressCodeCache ();
58
59 extern Block compile (git_uint32 pc);
60
61 typedef struct HashNode HashNode;
62
63 struct HashNode
64 {
65     git_uint32 address;      // Glulx address for this entry.
66     git_sint16 codeOffset;   // Offset in 4-byte words from this hash node to the compiled code.
67     git_sint16 headerOffset; // Offset in 4-byte words from this hash node to the block header.
68     union {
69         int pad;             // This pad assures that PatchNode and HashNode are the same size.
70         HashNode * next;     // Next node in the same hash table slot.
71     } u;
72 };
73
74 typedef struct BlockHeader
75 {
76     git_uint16 numHashNodes; // Number of lookup-able addresses in this block.
77     git_uint16 compiledSize; // Total size of this block, in 4-byte words.
78     git_uint32 glulxSize;    // Size of the glulx code this block represents, in bytes.
79     git_uint32 runCounter;   // Total number of opcodes executed in this block.
80 }                            // (used to determine which blocks stay in the cache)
81 BlockHeader;
82
83 // This is the header for the block currently being executed --
84 // that is, the one containing the return value of the last call
85 // to getCode().
86 extern BlockHeader * gBlockHeader;
87
88 // Hash table for code lookup -- inlined for speed
89
90 extern HashNode ** gHashTable; // Hash table of glulx address -> code.
91 extern git_uint32 gHashSize;   // Number of slots in the hash table.
92
93 GIT_INLINE Block getCode (git_uint32 pc)
94 {
95     HashNode * n = gHashTable [pc & (gHashSize-1)];
96     while (n)
97     {
98         if (n->address == pc)
99         {
100             gBlockHeader = (BlockHeader*) ((git_uint32*)n + n->headerOffset);
101             return (git_uint32*)n + n->codeOffset;
102         }
103         n = n->u.next;
104     }
105     return compile (pc);
106 }
107
108 #endif // GIT_COMPILER_H