read Blackboard data correctly from the SFC file, and construct a BlackboardPart...
[openc2e.git] / bytecode.h
blobfe6c75a74b87ec0c4065deb5f997f96ce1a82a5e
1 /*
2 * bytecode.h
3 * openc2e
5 * Created by Bryan Donlan on Thu 11 Aug 2005.
6 * Copyright (c) 2005-2006 Alyssa Milburn. All rights reserved.
7 * Copyright (c) 2005-2006 Bryan Donlan. All rights reserved.
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
20 #ifndef BYTECODE_H
21 #define BYTECODE_H 1
23 #include "openc2e.h"
25 #include <stdlib.h> // for NULL
26 #include <assert.h>
27 #include <string>
29 enum opcode_t {
30 /* IMPORTANT! The order of these elements must not change, or save
31 * compatibility will be broken.
33 * Note that relocated and non-relocated bytecode elements are treated
34 * seperately; you may add additional ones before CAOS_RELOCATABLE_BEGIN
35 * and before CAOS_INVALID.
37 /* Do nothing.
38 * Argument: (ignored)
40 CAOS_NOP = 0,
41 /* Abort the script.
42 * Argument: An index into the constants table, with a description of the
43 * error.
45 CAOS_DIE,
46 /* End the script gracefully.
47 * Argument: ignored
49 CAOS_STOP,
50 /* Invoke a caos command.
51 * Argument: An index into the commands table.
53 CAOS_CMD,
54 /* Pop two values A and B off the stack, as well as an integer flag C.
55 * Compare A and B with the given comparison mask, then push back C AND/OR
56 * the result, in accordance with the flag.
57 * Argument: A comparison flag
59 CAOS_COND,
60 /* Push a constant onto the stack.
61 * Argument: An index into the constants table
63 CAOS_CONST,
64 /* Push a constant integer in the range of a 24-bit int onto the stack.
65 * (ie, -2^24 <= x <= 2^24 - 1)
66 * Argument: An integer
68 CAOS_CONSTINT,
69 /* Push a bytestring onto the stack.
70 * Argument: An index into the bytestrings table.
72 CAOS_BYTESTR,
73 /* Copies the element (argument) elements from the top of the
74 * argument stack into the top of the aux stack. The values remain on
75 * the argument stack.
76 * Argument: A zero-based index into the argument stack
78 CAOS_PUSH_AUX,
79 /* Moves the top (argument) elements from the top of the aux
80 * stack into the top of the argument stack. This effectively reverses
81 * their order. The values are removed from the aux stack.
82 * Argument: The number of elements to move
84 CAOS_RESTORE_AUX,
85 /* Invokes the writeback handler for the given CAOS command.
86 * Argument: An index into the commands table.
88 CAOS_SAVE_CMD,
89 /* Exhausts the specified number of time slices.
90 * Argument: A number of time slices to spend. Can be negative or zero.
92 CAOS_YIELD,
93 /* Moves the top element on the value stack down (argument) places.
94 * Argument: How many places to move it down
96 CAOS_STACK_ROT,
97 /* Pseudo-instructions; marks the beginning of relocated ops. */
98 CAOS_NONRELOC_END,
99 CAOS_RELOCATABLE_BEGIN = 0x40,
100 /* Pop an integer off the stack. Jump to the given location if it's nonzero.
101 * Argument: A bytecode location (relocated).
102 * Cost: 0
104 CAOS_CJMP,
105 /* Jump to another location in the script.
106 * Argument: A bytecode location (relocated).
107 * Cost: 0
109 CAOS_JMP,
110 /* Pop a value off the stack. If it's nonzero, decrement, push back,
111 * and jump to the location in the argument.
112 * Argument: A bytecode location (relocated).
113 * Cost: 0
115 CAOS_DECJNZ,
116 /* Push the instruction pointer and value stack into the call stack,
117 * then jump to the given location.
118 * Argument: A bytecode location (relocated).
119 * Cost: 0
121 CAOS_GSUB,
122 /* Pop a value off the stack. If non-null, set targ to it, and go to the
123 * given address. Otherwise, set targ to ownr, and continue.
124 * Argument: A bytecode location (relocated).
125 * Cost: 0
127 CAOS_ENUMPOP,
129 CAOS_INVALID
132 static inline bool op_is_valid(opcode_t opcode) {
133 return (opcode >= 0 && opcode < CAOS_NONRELOC_END)
134 || (opcode > CAOS_RELOCATABLE_BEGIN && opcode < CAOS_INVALID);
137 static inline bool op_is_relocatable(opcode_t opcode) {
138 return (opcode > CAOS_RELOCATABLE_BEGIN && opcode < CAOS_INVALID);
141 struct caosOp {
142 enum opcode_t opcode : 8;
143 int argument : 24;
144 int traceindex; // -1 if unknown
146 caosOp(enum opcode_t oc, int arg, int ti){
147 assert(op_is_valid(oc));
148 assert(arg >= -(1 << 24) && arg < (1 << 24));
149 assert(ti >= -1);
150 opcode = oc;
151 argument = arg;
152 traceindex = ti;
154 private:
155 FRIEND_SERIALIZE(caosOp);
156 caosOp() { }
159 std::string dumpOp(const class Dialect *d, caosOp op);
161 // Condition classes
162 #define CEQ 1
163 #define CLT 2
164 #define CGT 4
165 #define CBT 8
166 #define CBF 16
167 #define CAND 32
168 #define COR 0
169 #define CMASK (CEQ | CLT | CGT | CBT | CBF)
170 #define CLE (CEQ | CLT)
171 #define CGE (CEQ | CGT)
172 #define CNE (CLT | CGT)
174 extern const char *cnams[];
176 #endif
177 /* vim: set noet: */