C1: deny drops into black holes
[openc2e.git] / bytecode.cpp
blob0a9e9d10e315e209c4ba17488306a5aee2f53064
1 /*
2 * bytecode.cpp
3 * openc2e
5 * Created by Bryan Donlan on Wed 07 Dec 2005.
6 * Copyright (c) 2005 Bryan Donlan. All rights reserved.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
19 #include "bytecode.h"
20 #include "dialect.h"
21 #include "cmddata.h"
22 #include <boost/format.hpp>
24 using boost::format;
25 using boost::str;
27 const char *cnams[] = {
28 NULL,
29 "EQ",
30 "LT",
31 "LE",
32 "GT",
33 "GE",
34 "NE",
35 NULL
38 static std::string try_lookup(const Dialect *d, int idx) {
39 if (d)
40 return std::string(d->getcmd(idx)->fullname);
41 return str(format("%d") % idx);
44 std::string dumpOp(const Dialect *d, caosOp op) {
45 int arg = op.argument; // weird C++ issues
46 switch (op.opcode) {
47 case CAOS_NOP:
48 return std::string("NOP");
49 case CAOS_DIE:
50 return str(format("DIE %d") % arg);
51 case CAOS_STOP:
52 return std::string("STOP");
53 case CAOS_CMD:
54 return str(format("CMD %s") % try_lookup(d, arg));
55 case CAOS_COND:
56 return str(format("COND %s %s") % (arg & CAND ? "AND" : "OR") % cnams[arg & CMASK]);
57 case CAOS_CONST:
58 return str(format("CONST %d") % arg);
59 case CAOS_CONSTINT:
60 return str(format("CONSTINT %d") % arg);
61 case CAOS_BYTESTR:
62 return str(format("BYTESTR %d") % arg);
63 case CAOS_PUSH_AUX:
64 return str(format("PUSH AUX %d") % arg);
65 case CAOS_RESTORE_AUX:
66 return str(format("RESTORE AUX %d") % arg);
67 case CAOS_SAVE_CMD:
68 return str(format("CMD SAVE %s") % try_lookup(d, arg));
69 case CAOS_YIELD:
70 return str(format("YIELD %d") % arg);
71 case CAOS_STACK_ROT:
72 return str(format("STACK ROT %d") % arg);
74 case CAOS_CJMP:
75 return str(format("CJMP %08d") % arg);
76 case CAOS_JMP:
77 return str(format("JMP %08d") % arg);
78 case CAOS_DECJNZ:
79 return str(format("DECJNZ %08d") % arg);
80 case CAOS_GSUB:
81 return str(format("GSUB %08d") % arg);
82 case CAOS_ENUMPOP:
83 return str(format("ENUMPOP %08d") % arg);
84 default:
85 return str(format("UNKNOWN %02x %06x") % arg);