Gyoji Compiler
Loading...
Searching...
No Matches
operations.hpp
1#pragma once
2
3#include <gyoji-misc/pointers.hpp>
4#include <gyoji-mir/types.hpp>
5#include <gyoji-context.hpp>
6
7#include <string>
8#include <map>
9#include <vector>
10
11namespace Gyoji::mir {
53 class Operation {
54 public:
72 typedef enum {
73 // Global symbols
124
125 // Cast operations
152 OP_WIDEN_SIGNED, // Widen a signed integer to a larger type.
153
178 OP_WIDEN_UNSIGNED, // Widen an unsigned integer to a larger type.
179
200 OP_WIDEN_FLOAT, // Widen a floating-point to a larger type.
201
222 OP_ARRAY_INDEX, // Pointer types -> Pointer Target type
223
245 OP_DOT, // class types -> Found member type
246
247 // Variable access
278
303
326
327 // Literals
396
409
429 OP_ADDRESSOF, // Any type -> Pointer type
445 OP_DEREFERENCE, // Pointer types -> Pointer Target type
446
453 OP_NEGATE, // Numeric types -> Numeric type
454
463 OP_BITWISE_NOT, // Integer types -> Integer Type
471 OP_LOGICAL_NOT, // Boolean types -> Boolean
472
480 OP_SIZEOF_TYPE, // Any type -> u64
481
482 // Binary operations: arithmetic
498 OP_ADD, // Matching Numeric types -> Operand type
514 OP_SUBTRACT, // Matching Numeric types -> Operand type
530 OP_MULTIPLY, // Matching Numeric types -> Operand type
549 OP_DIVIDE, // Matching Integer types -> Operand type
550
569 OP_MODULO, // Matching Integer types -> Operand type
570
571 // Binary operations: logical
580 OP_LOGICAL_AND, // Boolean types -> Boolean
589 OP_LOGICAL_OR, // Boolean types -> Boolean
590
591 // Binary operations: bitwise
604 OP_BITWISE_AND, // Matching integer types -> Integer type
617 OP_BITWISE_OR, // Matching integer types -> Integer type
618
631 OP_BITWISE_XOR, // Matching integer types -> Integer type
646 OP_SHIFT_LEFT, // Matching integer types -> Integer type
661 OP_SHIFT_RIGHT, // Matching integer types -> Integer type
662
663 // Binary operations: comparisons
674 OP_COMPARE_LESS, // Primitive types -> Boolean
685 OP_COMPARE_GREATER, // Primitive types -> Boolean
696 OP_COMPARE_LESS_EQUAL, // Primitive types -> Boolean
707 OP_COMPARE_GREATER_EQUAL, // Primitive types -> Boolean
718 OP_COMPARE_NOT_EQUAL, // Pointer types, Primitive types, recursive for class types.
729 OP_COMPARE_EQUAL, // Pointer types, Primitive types, recursive for class types.
730
731 // Binary operations: assignments
743 OP_ASSIGN, // Primitive types, pointer types, recursive for class types.
744
745 // Branch and flow control
758 OP_JUMP_CONDITIONAL, // Boolean types
791
796 Operation(
797 OperationType _type,
798 const Gyoji::context::SourceReference & _src_ref,
799 size_t _result
800 );
806 Operation(
807 OperationType _type,
808 const Gyoji::context::SourceReference & _src_ref,
809 size_t _result,
810 size_t _operand
811 );
817 Operation(
818 OperationType _type,
819 const Gyoji::context::SourceReference & _src_ref,
820 size_t _result,
821 size_t _operand_a,
822 size_t _operand_b
823 );
830 Operation(
831 OperationType _type,
832 const Gyoji::context::SourceReference & _src_ref,
833 size_t _result,
834 size_t _operand_a,
835 size_t _operand_b,
836 size_t _operand_c
837 );
844 virtual ~Operation();
845
850 void dump(FILE *out) const;
851
859 OperationType get_type() const;
860
868 const std::vector<size_t> & get_operands() const;
869
877 size_t get_result() const;
878
887 bool is_terminating() const;
888
899
911 virtual std::string get_description() const;
912 protected:
913 OperationType type;
914 const Gyoji::context::SourceReference & src_ref;
915 std::vector<size_t> operands;
916 size_t result;
917
926 void add_operand(size_t operand);
927 };
928
939 class OperationUnary : public Operation {
940 public:
948 OperationType _type,
949 const Gyoji::context::SourceReference & _src_ref,
950 size_t _result,
951 size_t _operand
952 );
959 virtual ~OperationUnary();
963 size_t get_a() const;
964 };
965
982 public:
992 OperationType _type,
993 const Gyoji::context::SourceReference & _src_ref,
994 size_t _result,
995 size_t _operand,
996 const Type *_cast_type
997 );
1004 virtual ~OperationCast();
1011 const Type* get_cast_type() const;
1012 protected:
1013 virtual std::string get_description() const;
1014 private:
1015 const Type *cast_type;
1016 };
1017
1029 public:
1038 OperationType _type,
1039 const Gyoji::context::SourceReference & _src_ref,
1040 size_t _result,
1041 size_t _operand_a,
1042 size_t _operand_b
1043 );
1050 virtual ~OperationBinary();
1054 size_t get_a() const;
1058 size_t get_b() const;
1059 };
1060
1070 public:
1080 const Gyoji::context::SourceReference & _src_ref,
1081 size_t _result,
1082 size_t _callee_tmpvar,
1083 std::vector<size_t> _arg_args
1084 );
1091 virtual ~OperationFunctionCall();
1092 };
1093
1103 public:
1112 const Gyoji::context::SourceReference & _src_ref,
1113 size_t _result,
1114 std::string _symbol_name
1115 );
1122 virtual ~OperationSymbol();
1127 const std::string & get_symbol_name() const;
1128
1129 protected:
1130 virtual std::string get_description() const;
1131 private:
1132 const std::string symbol_name;
1133 };
1134
1142 public:
1152 const Gyoji::context::SourceReference & _src_ref,
1153 size_t _result,
1154 size_t _array_tmpvar,
1155 size_t _index_tmpvar
1156 );
1163 virtual ~OperationArrayIndex();
1164 private:
1165 };
1166
1178 public:
1189 const Gyoji::context::SourceReference & _src_ref,
1190 size_t _result,
1191 size_t _operand,
1192 std::string _member_name
1193 );
1200 virtual ~OperationDot();
1205 const std::string & get_member_name() const;
1206
1207 protected:
1208 virtual std::string get_description() const;
1209 private:
1210 const std::string member_name;
1211 };
1212
1221 public:
1228 const Gyoji::context::SourceReference & _src_ref,
1229 size_t _result,
1230 std::string _symbol_name,
1231 const Type * _var_type);
1238 virtual ~OperationLocalVariable();
1239 const std::string & get_symbol_name() const;
1240 const Type * get_var_type() const;
1241
1242 protected:
1243 virtual std::string get_description() const;
1244 private:
1245 const std::string symbol_name;
1246 const Type * var_type;
1247 };
1248
1257 public:
1263 const Gyoji::context::SourceReference & _src_ref,
1264 size_t _result,
1265 char _literal_char
1266 );
1273 virtual ~OperationLiteralChar();
1274 char get_literal_char() const;
1275 protected:
1276 virtual std::string get_description() const;
1277 private:
1278 const char literal_char;
1279 };
1288 public:
1296 const Gyoji::context::SourceReference & _src_ref,
1297 size_t _result,
1298 std::string _literal_string
1299 );
1306 virtual ~OperationLiteralString();
1307 const std::string & get_literal_string() const;
1308 protected:
1309 virtual std::string get_description() const;
1310 private:
1311 const std::string literal_string;
1312 };
1326 public:
1334 const Gyoji::context::SourceReference & _src_ref,
1335 size_t _result,
1336 Type::TypeType _literal_type,
1337 unsigned char _literal_u8
1338 );
1346 const Gyoji::context::SourceReference & _src_ref,
1347 size_t _result,
1348 Type::TypeType _literal_type,
1349 unsigned short _literal_u16
1350 );
1358 const Gyoji::context::SourceReference & _src_ref,
1359 size_t _result,
1360 Type::TypeType _literal_type,
1361 unsigned int _literal_u32
1362 );
1370 const Gyoji::context::SourceReference & _src_ref,
1371 size_t _result,
1372 Type::TypeType _literal_type,
1373 unsigned long _literal_u64
1374 );
1382 const Gyoji::context::SourceReference & _src_ref,
1383 size_t _result,
1384 Type::TypeType _literal_type,
1385 char _literal_i8
1386 );
1394 const Gyoji::context::SourceReference & _src_ref,
1395 size_t _result,
1396 Type::TypeType _literal_type,
1397 short _literal_i16
1398 );
1406 const Gyoji::context::SourceReference & _src_ref,
1407 size_t _result,
1408 Type::TypeType _literal_type,
1409 int _literal_i32
1410 );
1418 const Gyoji::context::SourceReference & _src_ref,
1419 size_t _result,
1420 Type::TypeType _literal_type,
1421 long _literal_i64
1422 );
1429 virtual ~OperationLiteralInt();
1430 Type::TypeType get_literal_type() const;
1431 unsigned char get_literal_u8() const;
1432 unsigned short get_literal_u16() const;
1433 unsigned int get_literal_u32() const;
1434 unsigned long get_literal_u64() const;
1435
1436 char get_literal_i8() const;
1437 short get_literal_i16() const;
1438 int get_literal_i32() const;
1439 long get_literal_i64() const;
1440
1441 protected:
1442 virtual std::string get_description() const;
1443 private:
1444 Type::TypeType literal_type;
1445
1446 // Should these be a union?
1447 unsigned char literal_u8;
1448 unsigned short literal_u16;
1449 unsigned int literal_u32;
1450 unsigned long literal_u64;
1451 char literal_i8;
1452 short literal_i16;
1453 int literal_i32;
1454 long literal_i64;
1455 };
1464 public:
1471 const Gyoji::context::SourceReference & _src_ref,
1472 size_t _result,
1473 float _literal_float
1474 );
1481 const Gyoji::context::SourceReference & _src_ref,
1482 size_t _result,
1483 double _literal_float
1484 );
1491 virtual ~OperationLiteralFloat();
1492 float get_literal_float() const;
1493 double get_literal_double() const;
1494 Type::TypeType get_literal_type() const;
1495 protected:
1496 virtual std::string get_description() const;
1497 private:
1498 Type::TypeType literal_type;
1499 float literal_float_f32;
1500 double literal_float_f64;
1501
1502 };
1503
1513 public:
1519 const Gyoji::context::SourceReference & _src_ref,
1520 size_t _result,
1521 bool _literal_bool
1522 );
1529 virtual ~OperationLiteralBool();
1534 bool get_literal_bool() const;
1535 protected:
1536 virtual std::string get_description() const;
1537 private:
1538 bool literal_bool;
1539 };
1540
1550 public:
1556 const Gyoji::context::SourceReference & _src_ref,
1557 size_t _result
1558 );
1565 virtual ~OperationLiteralNull();
1566 protected:
1567 virtual std::string get_description() const;
1568 private:
1569 };
1570
1580 public:
1582 const Gyoji::context::SourceReference & _src_ref,
1583 size_t _result,
1584 const Type *_type
1585 );
1592 virtual ~OperationSizeofType();
1593
1594 const Type * get_type() const;
1595 private:
1596 const Type* type;
1597 };
1598
1620 public:
1622 const Gyoji::context::SourceReference & _src_ref,
1623 size_t _operand,
1624 size_t _if_block,
1625 size_t _else_block
1626 );
1633 virtual ~OperationJumpConditional();
1634 protected:
1635 virtual std::string get_description() const;
1636 private:
1637 };
1638
1655 class OperationJump : public Operation {
1656 public:
1658 const Gyoji::context::SourceReference & _src_ref,
1659 size_t _block
1660 );
1667 virtual ~OperationJump();
1668 protected:
1669 virtual std::string get_description() const;
1670 private:
1671 };
1672
1692 public:
1694 const Gyoji::context::SourceReference & _src_ref,
1695 size_t _operand
1696 );
1703 virtual ~OperationReturn();
1704 protected:
1705 virtual std::string get_description() const;
1706 };
1715 public:
1717 const Gyoji::context::SourceReference & _src_ref
1718 );
1725 virtual ~OperationReturnVoid();
1726 protected:
1727 virtual std::string get_description() const;
1728 };
1729
1743 public:
1745 const Gyoji::context::SourceReference & _src_ref,
1746 std::string _variable,
1747 const Type *_variable_type
1748 );
1755 virtual ~OperationLocalDeclare();
1759 const std::string & get_variable() const;
1764 const Type *get_variable_type() const;
1765 protected:
1766 virtual std::string get_description() const;
1767 private:
1768 std::string variable;
1769 const Type *variable_type;
1770 };
1781 public:
1783 const Gyoji::context::SourceReference & _src_ref,
1784 std::string _variable
1785 );
1792 virtual ~OperationLocalUndeclare();
1793 protected:
1794 virtual std::string get_description() const;
1795 private:
1796 std::string variable;
1797 };
1798
1799
1800 void operation_static_init();
1801};
References a location in the source-file.
Definition source-reference.hpp:16
This subclass of OperationBinary represents indexing an array.
Definition operations.hpp:1141
virtual ~OperationArrayIndex()
Move along, nothing to see here.
Definition operation.cpp:329
This subclass of Operation represents a binary operation.
Definition operations.hpp:1028
size_t get_a() const
Access the first operand (a).
Definition operation.cpp:260
virtual ~OperationBinary()
Move along, nothing to see here.
Definition operation.cpp:257
size_t get_b() const
Access the second operand (b).
Definition operation.cpp:264
This subclass of OperationUnary represents a cast operation.
Definition operations.hpp:981
virtual ~OperationCast()
Move along, nothing to see here.
Definition operation.cpp:224
virtual std::string get_description() const
Produce a description of the operation.
Definition operation.cpp:232
const Type * get_cast_type() const
Get type being cast/converted to.
Definition operation.cpp:228
This subclass of Operation is used to access member variables of classes and other aggregate types by...
Definition operations.hpp:1177
virtual ~OperationDot()
Move along, nothing to see here.
Definition operation.cpp:345
virtual std::string get_description() const
Produce a description of the operation.
Definition operation.cpp:353
const std::string & get_member_name() const
Definition operation.cpp:349
Function call (invoke) operation.
Definition operations.hpp:1069
virtual ~OperationFunctionCall()
Move along, nothing to see here.
Definition operation.cpp:284
This operation represents a conditional jump.
Definition operations.hpp:1619
virtual std::string get_description() const
Produce a description of the operation.
Definition operation.cpp:819
virtual ~OperationJumpConditional()
Move along, nothing to see here.
Definition operation.cpp:815
Unconditional Jump.
Definition operations.hpp:1655
virtual ~OperationJump()
Move along, nothing to see here.
Definition operation.cpp:841
virtual std::string get_description() const
Produce a description of the operation.
Definition operation.cpp:845
Literal bool.
Definition operations.hpp:1512
bool get_literal_bool() const
Definition operation.cpp:770
virtual ~OperationLiteralBool()
Move along, nothing to see here.
Definition operation.cpp:767
virtual std::string get_description() const
Produce a description of the operation.
Definition operation.cpp:773
Literal character.
Definition operations.hpp:1256
virtual ~OperationLiteralChar()
Move along, nothing to see here.
Definition operation.cpp:413
virtual std::string get_description() const
Produce a description of the operation.
Definition operation.cpp:421
Literal float.
Definition operations.hpp:1463
virtual ~OperationLiteralFloat()
Move along, nothing to see here.
Definition operation.cpp:717
virtual std::string get_description() const
Produce a description of the operation.
Definition operation.cpp:732
Literal integer.
Definition operations.hpp:1325
virtual ~OperationLiteralInt()
Move along, nothing to see here.
Definition operation.cpp:610
virtual std::string get_description() const
Produce a description of the operation.
Definition operation.cpp:642
Literal null.
Definition operations.hpp:1549
virtual std::string get_description() const
Produce a description of the operation.
Definition operation.cpp:794
virtual ~OperationLiteralNull()
Move along, nothing to see here.
Definition operation.cpp:791
Literal string.
Definition operations.hpp:1287
virtual std::string get_description() const
Produce a description of the operation.
Definition operation.cpp:455
virtual ~OperationLiteralString()
Move along, nothing to see here.
Definition operation.cpp:447
Declare a local variable in scope.
Definition operations.hpp:1742
virtual std::string get_description() const
Produce a description of the operation.
Definition operation.cpp:921
const Type * get_variable_type() const
Definition operation.cpp:917
const std::string & get_variable() const
Definition operation.cpp:914
virtual ~OperationLocalDeclare()
Move along, nothing to see here.
Definition operation.cpp:911
Un-declare a variable (remove it from scope)
Definition operations.hpp:1780
virtual ~OperationLocalUndeclare()
Move along, nothing to see here.
Definition operation.cpp:943
virtual std::string get_description() const
Produce a description of the operation.
Definition operation.cpp:947
Load a local variable.
Definition operations.hpp:1220
virtual std::string get_description() const
Produce a description of the operation.
Definition operation.cpp:390
virtual ~OperationLocalVariable()
Move along, nothing to see here.
Definition operation.cpp:378
Return from a function without a value.
Definition operations.hpp:1714
virtual ~OperationReturnVoid()
Move along, nothing to see here.
Definition operation.cpp:888
virtual std::string get_description() const
Produce a description of the operation.
Definition operation.cpp:892
Return from a function with a value.
Definition operations.hpp:1691
virtual ~OperationReturn()
Move along, nothing to see here.
Definition operation.cpp:865
virtual std::string get_description() const
Produce a description of the operation.
Definition operation.cpp:869
The operation for obtaining the storage size of a type.
Definition operations.hpp:1579
virtual ~OperationSizeofType()
Move along, nothing to see here.
Definition operation.cpp:968
Symbol-table lookup.
Definition operations.hpp:1102
const std::string & get_symbol_name() const
Definition operation.cpp:303
virtual std::string get_description() const
Produce a description of the operation.
Definition operation.cpp:307
virtual ~OperationSymbol()
Move along, nothing to see here.
Definition operation.cpp:299
This subclass of Operation represents a unary operation.
Definition operations.hpp:939
size_t get_a() const
Returns the operand of the unary operation.
Definition operation.cpp:209
virtual ~OperationUnary()
Move along, nothing to see here.
Definition operation.cpp:206
Operations inside basic blocks, the virtual instruction-set of the MIR.
Definition operations.hpp:53
virtual ~Operation()
Move along, nothing to see here.
Definition operation.cpp:138
bool is_terminating() const
Returns true if this is a terminating operation for a block.
Definition operation.cpp:162
OperationType get_type() const
Opcode of this operation.
Definition operation.cpp:173
const Gyoji::context::SourceReference & get_source_ref() const
Get the reference to the source which originated this operation.
Definition operation.cpp:185
virtual std::string get_description() const
Produce a description of the operation.
Definition operation.cpp:148
size_t get_result() const
Get the result of this operation.
Definition operation.cpp:181
void add_operand(size_t operand)
Add an operand.
Definition operation.cpp:142
OperationType
Operations of the MIR virtual-machine.
Definition operations.hpp:72
@ OP_DEREFERENCE
De-references the given pointer.
Definition operations.hpp:445
@ OP_LITERAL_STRING
Loads a string constant.
Definition operations.hpp:358
@ OP_ASSIGN
Assign value.
Definition operations.hpp:743
@ OP_BITWISE_AND
Bitwise AND.
Definition operations.hpp:604
@ OP_LOCAL_UNDECLARE
Undeclare local variable.
Definition operations.hpp:302
@ OP_NEGATE
Negates a signed integer.
Definition operations.hpp:453
@ OP_ADD
Arithmetic add.
Definition operations.hpp:498
@ OP_WIDEN_FLOAT
Widen a floating-point integer to wider type.
Definition operations.hpp:200
@ OP_LITERAL_CHAR
Loads a literal character constant.
Definition operations.hpp:339
@ OP_MODULO
Modulo.
Definition operations.hpp:569
@ OP_WIDEN_SIGNED
Widen a signed integer to a larger type.
Definition operations.hpp:152
@ OP_ADDRESSOF
Returns the address of the given variable.
Definition operations.hpp:429
@ OP_COMPARE_GREATER
Compare numbers for greater-than.
Definition operations.hpp:685
@ OP_LITERAL_FLOAT
Loads an integer constant.
Definition operations.hpp:384
@ OP_WIDEN_UNSIGNED
Widen an unsigned integer to a larger type.
Definition operations.hpp:178
@ OP_ARRAY_INDEX
Access a value inside an array.
Definition operations.hpp:222
@ OP_BITWISE_XOR
Bitwise XOR.
Definition operations.hpp:631
@ OP_LOCAL_VARIABLE
Load value from variable.
Definition operations.hpp:325
@ OP_RETURN
Return from function.
Definition operations.hpp:780
@ OP_SUBTRACT
Arithmetic subtract.
Definition operations.hpp:514
@ OP_LOGICAL_NOT
This opcode performs a boolean NOT of the given operand.
Definition operations.hpp:471
@ OP_BITWISE_NOT
Performs a bitwise not on an unsigned integer.
Definition operations.hpp:463
@ OP_COMPARE_EQUAL
Compare numbers for equality.
Definition operations.hpp:729
@ OP_RETURN_VOID
Return from function without supplying a value.
Definition operations.hpp:789
@ OP_BITWISE_OR
Bitwise OR.
Definition operations.hpp:617
@ OP_SYMBOL
Load a symbol from the symbol-table.
Definition operations.hpp:123
@ OP_SIZEOF_TYPE
Returns the size of a specific type.
Definition operations.hpp:480
@ OP_SHIFT_RIGHT
Bitwise Shift Right.
Definition operations.hpp:661
@ OP_LITERAL_INT
Loads an integer constant.
Definition operations.hpp:371
@ OP_JUMP_CONDITIONAL
Conditional Jump.
Definition operations.hpp:758
@ OP_LITERAL_NULL
Loads a null pointer literal constant.
Definition operations.hpp:408
@ OP_COMPARE_LESS_EQUAL
Compare numbers for less-than or equal to.
Definition operations.hpp:696
@ OP_COMPARE_GREATER_EQUAL
Compare numbers for greater-than or equal to.
Definition operations.hpp:707
@ OP_LOCAL_DECLARE
Declare local variable.
Definition operations.hpp:277
@ OP_LOGICAL_AND
Logical AND.
Definition operations.hpp:580
@ OP_DOT
Access a value inside a class.
Definition operations.hpp:245
@ OP_SHIFT_LEFT
Bitwise Shift left.
Definition operations.hpp:646
@ OP_COMPARE_LESS
Compare numbers for less-than.
Definition operations.hpp:674
@ OP_FUNCTION_CALL
Function call.
Definition operations.hpp:97
@ OP_LITERAL_BOOL
Loads a boolean literal value.
Definition operations.hpp:395
@ OP_DIVIDE
Arithmetic multiply.
Definition operations.hpp:549
@ OP_LOGICAL_OR
Logical OR.
Definition operations.hpp:589
@ OP_MULTIPLY
Arithmetic multiply.
Definition operations.hpp:530
@ OP_COMPARE_NOT_EQUAL
Compare numbers for not equal-to.
Definition operations.hpp:718
@ OP_JUMP
Jump (Unconditional)
Definition operations.hpp:767
const std::vector< size_t > & get_operands() const
Get the operands.
Definition operation.cpp:177
void dump(FILE *out) const
Definition operation.cpp:189
This represents a type as declared in a translation unit.
Definition types.hpp:299
TypeType
Type of type.
Definition types.hpp:321
Middle intermediate representation (MIR) of a translation unit.
Definition gyoji-mir.hpp:37