V-Gears 0
Free Final Fantasy VII engine.
Instruction.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2022 The V-Gears Team
3 *
4 * This file is part of V-Gears
5 *
6 * V-Gears is free software: you can redistribute it and/or modify it under
7 * terms of the GNU General Public License as published by the Free Software
8 * Foundation, version 3.0 (GPLv3) of the License.
9 *
10 * V-Gears is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16#pragma once
17
18#include <string>
19#include <vector>
20#include <boost/format.hpp>
21#include <boost/intrusive_ptr.hpp>
26
27class CodeGenerator;
28
29class Engine;
30
36void SetOutputStackEffect(bool value);
37
38class Instruction;
39
43typedef boost::intrusive_ptr<Instruction> InstPtr;
44
45class Function;
46
50class Instruction : public RefCounted {
51
52 public:
53
58
63
67 static int INST_TYPE_CALL;
68
73
77 static int INST_TYPE_DUP;
78
82 static int INST_TYPE_JUMP;
83
88
92 static int INST_TYPE_LOAD;
93
97 static int INST_TYPE_RETURN;
98
102 static int INST_TYPE_STACK;
103
107 static int INST_TYPE_STORE;
108
113
118
126 friend std::ostream& operator<<(std::ostream &output, const Instruction *inst) {
127 return inst->Print(output);
128 }
129
136 virtual std::ostream& Print(std::ostream &output) const;
137
143 virtual bool IsJump() const;
144
151 virtual bool IsCondJump() const;
152
159 virtual bool IsUncondJump() const;
160
167 virtual bool IsStackOp() const;
168
175 virtual bool IsFuncCall() const;
176
183 virtual bool IsReturn() const;
184
191 virtual bool IsKernelCall() const;
192
199 virtual bool IsLoad() const;
200
207 virtual bool IsStore() const;
208
215 virtual uint32 GetDestAddress() const;
216
225 virtual void ProcessInst(
226 Function& function, ValueStack& stack, Engine* engine, CodeGenerator* code_gen
227 ) = 0;
228
234 uint32 GetOpcode() const;
235
241 void SetOpcode(uint32 opcode);
242
248 uint32 GetAddress() const;
249
255 void SetAddress(uint32 address);
256
262 std::string GetName() const;
263
264 void SetName(std::string name);
265
271 int16 GetStackChange() const;
272
279 void SetStackChange(int16 stack_change);
280
286 std::vector<ValuePtr> GetParams() const;
287
295 ValuePtr GetParam(uint32 index) const;
296
302 void SetParams(std::vector<ValuePtr> params);
303
309 void AddParam(ValuePtr value);
310
318 std::string GetCodeGenData() const;
319
327 void SetCodeGenData(std::string code_gen_data);
328
332 bool LabelRequired() const;
333
337 void SetLabelRequired(bool required);
338
339 protected:
340
345
350
354 std::string name_;
355
360
364 std::vector<ValuePtr> params_;
365
371 std::string code_gen_data_;
372
376 bool label_required_ = false;
377};
378
382typedef std::vector<InstPtr> InstVec;
383
387typedef InstVec::iterator InstIterator;
388
392typedef InstVec::const_iterator ConstInstIterator;
void SetOutputStackEffect(bool value)
Changes whether or not to output the stack effect for an instruction.
Definition: Instruction.cpp:22
InstVec::iterator InstIterator
Type representing an iterator over InstPtrs.
Definition: Instruction.h:387
boost::intrusive_ptr< Instruction > InstPtr
Pointer to an Instruction.
Definition: Instruction.h:43
InstVec::const_iterator ConstInstIterator
Type representing a const_iterator over InstPtrs.
Definition: Instruction.h:392
std::vector< InstPtr > InstVec
Type representing a vector of InstPtrs.
Definition: Instruction.h:382
boost::intrusive_ptr< Value > ValuePtr
Pointer to a Value.
Definition: Value.h:93
Base class for code generators.
Definition: CodeGenerator.h:58
Base class for engines.
Definition: Engine.h:34
Structure for representing an instruction.
Definition: Instruction.h:50
std::string code_gen_data_
String containing metadata for code generation.
Definition: Instruction.h:371
friend std::ostream & operator<<(std::ostream &output, const Instruction *inst)
Operator overload to output an Instruction to a stream.
Definition: Instruction.h:126
virtual void ProcessInst(Function &function, ValueStack &stack, Engine *engine, CodeGenerator *code_gen)=0
Process an instruction for code generation.
uint32 GetAddress() const
Retrieves the instruction address.
Definition: Instruction.cpp:85
virtual bool IsStore() const
Checks if the instruction is a store operation.
Definition: Instruction.cpp:77
static int INST_TYPE_JUMP
Unconditional jump.
Definition: Instruction.h:82
virtual uint32 GetDestAddress() const
Returns the destination address of a jump instruction.
Definition: Instruction.cpp:79
static int INST_TYPE_CALL
Regular function call.
Definition: Instruction.h:67
static int INST_TYPE_UNARY_OP_PRE
Unary operation (e.g.
Definition: Instruction.h:112
static int INST_TYPE_STACK
Stack allocation or deallocation (altering stack pointer).
Definition: Instruction.h:102
virtual bool IsJump() const
Checks if the instruction is a jump of some sort.
Definition: Instruction.cpp:61
virtual bool IsUncondJump() const
Checks if the instruction is an unconditional jump.
Definition: Instruction.cpp:65
std::string name_
The instruction name (opcode name).
Definition: Instruction.h:354
int16 GetStackChange() const
Checks how much the instruction changes the stack pointer.
Definition: Instruction.cpp:93
static int INST_TYPE_COND_JUMP
Conditional jump.
Definition: Instruction.h:72
static int INST_TYPE_LOAD
Load value from memory.
Definition: Instruction.h:92
uint32 GetOpcode() const
Retrieves the instruction opcode.
Definition: Instruction.cpp:81
void SetAddress(uint32 address)
Sets the instruction address.
Definition: Instruction.cpp:87
bool LabelRequired() const
Checks if the instruction requires a label.
Definition: Instruction.cpp:112
std::string GetName() const
Retrieves the instruction name (the opcode name).
Definition: Instruction.cpp:89
static int INST_TYPE_UNARY_OP_POST
Unary operation with operator placed after the operator.
Definition: Instruction.h:117
virtual bool IsFuncCall() const
Checks if the instruction is a call to a script function.
Definition: Instruction.cpp:69
std::string GetCodeGenData() const
Retrieves metadata for code generation.
Definition: Instruction.cpp:108
virtual bool IsKernelCall() const
Checks if the instruction is a call to a kernel function.
Definition: Instruction.cpp:73
static int INST_TYPE_BOOL_NEGATE
Boolean negation.
Definition: Instruction.h:62
virtual bool IsCondJump() const
Checks if the instruction is a conditional jump.
Definition: Instruction.cpp:63
static int INST_TYPE_STORE
Store value in memory.
Definition: Instruction.h:107
std::vector< ValuePtr > params_
Array of parameters used for the instruction.
Definition: Instruction.h:364
virtual bool IsLoad() const
Checks if the instruction is a load operation.
Definition: Instruction.cpp:75
int16 stack_change_
How much this instruction changes the stack pointer by.
Definition: Instruction.h:359
void SetStackChange(int16 stack_change)
Defines how much the instruction changes the stack pointer.
Definition: Instruction.cpp:95
ValuePtr GetParam(uint32 index) const
Retrieves a instruction parameter.
Definition: Instruction.cpp:99
uint32 opcode_
The instruction opcode.
Definition: Instruction.h:344
void SetCodeGenData(std::string code_gen_data)
Sets metadata for code generation.
Definition: Instruction.cpp:110
void SetParams(std::vector< ValuePtr > params)
Sets the instructions parameters.
Definition: Instruction.cpp:104
void AddParam(ValuePtr value)
Adds a parameter to the instructions.
Definition: Instruction.cpp:106
static int INST_TYPE_RETURN
Return from regular function call.
Definition: Instruction.h:97
void SetLabelRequired(bool required)
Indicates if the instruction needs a label.
Definition: Instruction.cpp:114
virtual bool IsReturn() const
Checks if the instruction is a return statement.
Definition: Instruction.cpp:71
uint32 address_
The instruction address.
Definition: Instruction.h:349
void SetName(std::string name)
Definition: Instruction.cpp:91
static int INST_TYPE_BINARY_OP
Binary operation (e.g.
Definition: Instruction.h:57
bool label_required_
Indicates if a label is required.
Definition: Instruction.h:376
std::vector< ValuePtr > GetParams() const
Retrieves the list of instruction parameters.
Definition: Instruction.cpp:97
static int INST_TYPE_DUP
Instruction duplicates the most recent stack entry.
Definition: Instruction.h:77
void SetOpcode(uint32 opcode)
Sets the instruction opcode.
Definition: Instruction.cpp:83
static int INST_TYPE_KERNEL_CALL
Kernel functions.
Definition: Instruction.h:87
virtual std::ostream & Print(std::ostream &output) const
Print the instruction to a stream.
Definition: Instruction.cpp:50
virtual bool IsStackOp() const
Checks if the instruction is a stack operation.
Definition: Instruction.cpp:67
Provides a base implementation of reference counting.
Definition: RefCounted.h:28
unsigned int uint32
Definition: scummsys.h:435
signed short int16
Definition: scummsys.h:434
Structure representing a function.
Definition: Function.h:26