Intel® Implicit SPMD Program Compiler (Intel® ISPC)  1.13.0
ast.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2011-2019, Intel Corporation
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions are
7  met:
8 
9  * Redistributions of source code must retain the above copyright
10  notice, this list of conditions and the following disclaimer.
11 
12  * Redistributions in binary form must reproduce the above copyright
13  notice, this list of conditions and the following disclaimer in the
14  documentation and/or other materials provided with the distribution.
15 
16  * Neither the name of Intel Corporation nor the names of its
17  contributors may be used to endorse or promote products derived from
18  this software without specific prior written permission.
19 
20 
21  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
22  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
24  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
25  OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33 
34 /** @file ast.h
35  @brief
36 */
37 
38 #pragma once
39 
40 #include "ispc.h"
41 #include <vector>
42 
43 /** @brief Abstract base class for nodes in the abstract syntax tree (AST).
44 
45  This class defines a basic interface that all abstract syntax tree
46  (AST) nodes must implement. The base classes for both expressions
47  (Expr) and statements (Stmt) inherit from this class.
48 */
49 class ASTNode {
50  const unsigned char SubclassID; // Subclass identifier (for isa/dyn_cast)
51  public:
52  ASTNode(SourcePos p, unsigned scid) : SubclassID(scid), pos(p) {}
53  virtual ~ASTNode();
54 
55  /** The Optimize() method should perform any appropriate early-stage
56  optimizations on the node (e.g. constant folding). This method
57  will be called after the node's children have already been
58  optimized, and the caller will store the returned ASTNode * in
59  place of the original node. This method should return NULL if an
60  error is encountered during optimization. */
61  virtual ASTNode *Optimize() = 0;
62 
63  /** Type checking should be performed by the node when this method is
64  called. In the event of an error, a NULL value may be returned.
65  As with ASTNode::Optimize(), the caller should store the returned
66  pointer in place of the original ASTNode *. */
67  virtual ASTNode *TypeCheck() = 0;
68 
69  /** Estimate the execution cost of the node (not including the cost of
70  the children. The value returned should be based on the COST_*
71  enumerant values defined in ispc.h. */
72  virtual int EstimateCost() const = 0;
73 
74  /** All AST nodes must track the file position where they are
75  defined. */
77 
78  /** An enumeration for keeping track of the concrete subclass of Value
79  that is actually instantiated.*/
80  enum ASTNodeTy {
81  /* For classes inherited from Expr */
104  /* This is a convenience separator to shorten classof implementations */
106  /* For classes inherited from Stmt */
128  };
129 
130  /** Return an ID for the concrete type of this object. This is used to
131  implement the classof checks. This should not be used for any
132  other purpose, as the values may change as ISPC evolves */
133  unsigned getValueID() const { return SubclassID; }
134 
135  static inline bool classof(ASTNode const *) { return true; }
136 };
137 
138 class AST {
139  public:
140  /** Add the AST for a function described by the given declaration
141  information and source code. */
142  void AddFunction(Symbol *sym, Stmt *code);
143 
144  /** Generate LLVM IR for all of the functions into the current
145  module. */
146  void GenerateIR();
147 
148  private:
149  std::vector<Function *> functions;
150 };
151 
152 /** Callback function type for preorder traversial visiting function for
153  the AST walk.
154  */
155 typedef bool (*ASTPreCallBackFunc)(ASTNode *node, void *data);
156 
157 /** Callback function type for postorder traversial visiting function for
158  the AST walk.
159  */
160 typedef ASTNode *(*ASTPostCallBackFunc)(ASTNode *node, void *data);
161 
162 /** Walk (some portion of) an AST, starting from the given root node. At
163  each node, if preFunc is non-NULL, call it, passing the given void
164  *data pointer; if the call to preFunc function returns false, then the
165  children of the node aren't visited. This function then makes
166  recursive calls to WalkAST() to process the node's children; after
167  doing so, calls postFunc, at the node. The return value from the
168  postFunc call is ignored. */
169 extern ASTNode *WalkAST(ASTNode *root, ASTPreCallBackFunc preFunc, ASTPostCallBackFunc postFunc, void *data);
170 
171 /** Perform simple optimizations on the AST or portion thereof passed to
172  this function, returning the resulting AST. */
173 extern ASTNode *Optimize(ASTNode *root);
174 
175 /** Convenience version of Optimize() for Expr *s that returns an Expr *
176  (rather than an ASTNode *, which would require the caller to cast back
177  to an Expr *). */
178 extern Expr *Optimize(Expr *);
179 
180 /** Convenience version of Optimize() for Expr *s that returns an Stmt *
181  (rather than an ASTNode *, which would require the caller to cast back
182  to a Stmt *). */
183 extern Stmt *Optimize(Stmt *);
184 
185 /** Perform type-checking on the given AST (or portion of one), returning a
186  pointer to the root of the resulting AST. */
187 extern ASTNode *TypeCheck(ASTNode *root);
188 
189 /** Convenience version of TypeCheck() for Expr *s that returns an Expr *. */
190 extern Expr *TypeCheck(Expr *);
191 
192 /** Convenience version of TypeCheck() for Stmt *s that returns an Stmt *. */
193 extern Stmt *TypeCheck(Stmt *);
194 
195 /** Returns an estimate of the execution cost of the tree starting at
196  the given root. */
197 extern int EstimateCost(ASTNode *root);
198 
199 /** Returns true if it would be safe to run the given code with an "all
200  off" mask. */
201 extern bool SafeToRunWithMaskAllOff(ASTNode *root);
virtual int EstimateCost() const =0
ASTNode(SourcePos p, unsigned scid)
Definition: ast.h:52
Definition: ast.h:138
virtual ASTNode * TypeCheck()=0
Interface class for statements in the ispc language.
Definition: stmt.h:48
const unsigned char SubclassID
Definition: ast.h:50
ASTNode * WalkAST(ASTNode *root, ASTPreCallBackFunc preFunc, ASTPostCallBackFunc postFunc, void *data)
Definition: ast.cpp:68
Abstract base class for nodes in the abstract syntax tree (AST).
Definition: ast.h:49
unsigned getValueID() const
Definition: ast.h:133
bool SafeToRunWithMaskAllOff(ASTNode *root)
Definition: ast.cpp:417
virtual ASTNode * Optimize()=0
Representation of a range of positions in a source file.
Definition: ispc.h:123
bool(* ASTPreCallBackFunc)(ASTNode *node, void *data)
Definition: ast.h:155
SourcePos pos
Definition: ast.h:76
static bool classof(ASTNode const *)
Definition: ast.h:135
std::vector< Function * > functions
Definition: ast.h:149
Representation of a program symbol.
Definition: sym.h:62
ASTNode *(* ASTPostCallBackFunc)(ASTNode *node, void *data)
Definition: ast.h:160
Expr is the abstract base class that defines the interface that all expression types must implement...
Definition: expr.h:47
ASTNodeTy
Definition: ast.h:80
Main ispc.header file. Defines Target, Globals and Opt classes.
virtual ~ASTNode()
Definition: ast.cpp:50