tan  0.0.1
intrinsic.h
1 #ifndef __TAN_INCLUDE_INTRINSIC_H__
2 #define __TAN_INCLUDE_INTRINSIC_H__
3 #include "expr.h"
4 #include "ast_named.h"
5 #include "stmt.h"
6 #include <memory>
7 
8 namespace llvm {
9 class Value;
10 class Function;
11 } // namespace llvm
12 
13 namespace tanlang {
14 
15 enum class IntrinsicType {
16  INVALID = 0,
17 
18  ABORT, /// abort
19  ASM, /// inline assembly
20  SWAP, /// swap, atomic for primitive types
21  MEMSET, /// memset
22  MEMCPY, /// memcpy
23  RANGE, /// range of integers
24  COMP_PRINT, /// compile-time print
25  FILENAME, /// name of a source file
26  LINENO, /// line number of certain code
27  DEFINE, /// macro definition
28  LIKELY, /// likely
29  UNLIKELY, /// unlikely
30  NOOP, /// no-op
31  EXPECT, /// expect a value to be true, used in tests
32 
33  // type support
34  SIZE_OF, /// size of a type, in bytes
35  OFFSET_OF, /// offset of a member variable inside a struct, in bytes
36  ALIGN_OF, /// alignment size of a pointer, array or struct, in bytes
37  ISA, /// test if a type is equivalent to another one
38 
39  // numeric support
40  MIN_OF, /// minimum value of an integer type
41  MAX_OF, /// maximum value of an integer type
42  IS_UNSIGNED, /// test if a type is unsigned
43 
44  // test related
45  TEST_COMP_ERROR, /// A block of code that are expected to cause a compile error
46 
47  // others
48  GET_DECL, /// get source code of the declaration
49  STACK_TRACE, /// print stack trace
50 };
51 
52 /**
53  * \brief A generic representation of Intrinsic variables/functions
54  */
55 class Intrinsic : public Expr, public ASTNamed {
56 protected:
57  explicit Intrinsic(TokenizedSourceFile *src);
58 
59 public:
60  static Intrinsic *Create(TokenizedSourceFile *src);
61 
62  /**
63  * \brief A mapping from intrinsic names to IntrinsicType
64  */
65  static umap<str, IntrinsicType> INTRINSIC_NAME_TO_TYPES;
66 
67  /**
68  * \brief Generate a list of intrinsics function prototypes/declarations, such as `@abort`
69  */
70  static vector<FunctionDecl *> GetIntrinsicFunctionDeclarations();
71 
72  static inline const str STACK_TRACE_FUNCTION_REAL_NAME = "__tan_runtime_stack_trace";
73  static inline const str TEST_COMP_ERROR_NAME = "test_comp_error";
74  static inline const str ABORT_NAME = "abort";
75  static inline const str COMP_PRINT_NAME = "compprint";
76 
77 public:
78  IntrinsicType get_intrinsic_type() const;
79  void set_intrinsic_type(IntrinsicType intrinsic_type);
80  ASTBase *get_sub() const;
81  void set_sub(ASTBase *sub);
82 
83  vector<ASTBase *> get_children() const override;
84 
85 public:
86  str terminal_token() const override;
87 
88 private:
89  IntrinsicType _intrinsic_type = IntrinsicType::INVALID;
90  ASTBase *_sub = nullptr;
91 };
92 
93 class TestCompError : public CompoundStmt {
94 public:
95  explicit TestCompError(TokenizedSourceFile *src) : CompoundStmt(src) {}
96 
97  bool _caught = false;
98 };
99 
100 } // namespace tanlang
101 
102 #endif /* __TAN_INCLUDE_INTRINSIC_H__ */
All named AST nodes should inherit this class.
Definition: ast_named.h:10
A generic representation of Intrinsic variables/functions.
Definition: intrinsic.h:55
static umap< str, IntrinsicType > INTRINSIC_NAME_TO_TYPES
A mapping from intrinsic names to IntrinsicType.
Definition: intrinsic.h:65
static vector< FunctionDecl * > GetIntrinsicFunctionDeclarations()
Generate a list of intrinsics function prototypes/declarations, such as @abort
Definition: intrinsic.cpp:35
vector< ASTBase * > get_children() const override
Get a ordered list of child nodes.
Definition: intrinsic.cpp:67
str terminal_token() const override
Which terminal token is expected immediately after this node.
Definition: intrinsic.cpp:74
Different from SourceFile, TokenizedSourceFile manages the tokenized text of a source file.