Nearly CC
An educational compiler skeleton
instruction.h
1 // Copyright (c) 2021-2023, David H. Hovemeyer <david.hovemeyer@gmail.com>
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the "Software"),
5 // to deal in the Software without restriction, including without limitation
6 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
7 // and/or sell copies of the Software, and to permit persons to whom the
8 // Software is furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included
11 // in all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
17 // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
18 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
19 // OTHER DEALINGS IN THE SOFTWARE.
20 
21 #ifndef INSTRUCTION_H
22 #define INSTRUCTION_H
23 
24 #include <string>
25 #include "symtab.h"
26 #include "operand.h"
27 
31 class Instruction {
32 private:
33  int m_opcode;
34  unsigned m_num_operands;
35  Operand m_operands[3];
36  std::string m_comment;
37  Symbol *m_symbol;
38 
39 public:
44  Instruction(int opcode);
45 
49  Instruction(int opcode, const Operand &op1);
50 
55  Instruction(int opcode, const Operand &op1, const Operand &op2);
56 
63  Instruction(int opcode, const Operand &op1, const Operand &op2, const Operand &op3, unsigned num_operands = 3);
64 
65  ~Instruction();
66 
69  Instruction *duplicate() const { return new Instruction(*this); }
70 
76  int get_opcode() const;
77 
80  unsigned get_num_operands() const;
81 
85  const Operand &get_operand(unsigned index) const;
86 
90  void set_operand(unsigned index, const Operand &operand);
91 
94  Operand get_last_operand() const;
95 
104  void set_comment(const std::string &comment) { m_comment = comment; }
105 
108  bool has_comment() const { return !m_comment.empty(); }
109 
112  const std::string &get_comment() const { return m_comment; }
113 
119  void set_symbol(Symbol *sym) { m_symbol = sym; }
120 
124  Symbol *get_symbol() const { return m_symbol; }
125 };
126 
127 #endif // INSTRUCTION_H
Instruction object type.
Definition: instruction.h:31
const std::string & get_comment() const
Get the textual comment for this Instruction.
Definition: instruction.h:112
const Operand & get_operand(unsigned index) const
Get the specified Operand.
Definition: instruction.cpp:54
void set_comment(const std::string &comment)
Set a textual comment for this Instruction.
Definition: instruction.h:104
Instruction(int opcode)
Contructor from opcode.
Definition: instruction.cpp:24
Operand get_last_operand() const
Return a copy of the last (rightmost) Operand.
Definition: instruction.cpp:65
void set_operand(unsigned index, const Operand &operand)
Modify the specified Operand.
Definition: instruction.cpp:60
Symbol * get_symbol() const
Return the symbol table entry (Symbol).
Definition: instruction.h:124
void set_symbol(Symbol *sym)
Set a symbol table entry (Symbol).
Definition: instruction.h:119
Instruction * duplicate() const
Return an exact duplicate of this Instruction.
Definition: instruction.h:69
unsigned get_num_operands() const
Get the number of operands.
Definition: instruction.cpp:50
int get_opcode() const
Get the opcode value.
Definition: instruction.cpp:46
bool has_comment() const
Check whether this Instruction has a comment set.
Definition: instruction.h:108
Definition: operand.h:31
Definition: symbol.h:28