Intel SPMD Program Compiler  1.9.1
ast.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2011-2013, 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 #ifndef ISPC_AST_H
39 #define ISPC_AST_H 1
40 
41 #include "ispc.h"
42 #include <vector>
43 
44 /** @brief Abstract base class for nodes in the abstract syntax tree (AST).
45 
46  This class defines a basic interface that all abstract syntax tree
47  (AST) nodes must implement. The base classes for both expressions
48  (Expr) and statements (Stmt) inherit from this class.
49 */
50 class ASTNode {
51  const unsigned char SubclassID; // Subclass identifier (for isa/dyn_cast)
52 public:
53  ASTNode(SourcePos p, unsigned scid) : SubclassID(scid), pos(p) { }
54  virtual ~ASTNode();
55 
56  /** The Optimize() method should perform any appropriate early-stage
57  optimizations on the node (e.g. constant folding). This method
58  will be called after the node's children have already been
59  optimized, and the caller will store the returned ASTNode * in
60  place of the original node. This method should return NULL if an
61  error is encountered during optimization. */
62  virtual ASTNode *Optimize() = 0;
63 
64  /** Type checking should be performed by the node when this method is
65  called. In the event of an error, a NULL value may be returned.
66  As with ASTNode::Optimize(), the caller should store the returned
67  pointer in place of the original ASTNode *. */
68  virtual ASTNode *TypeCheck() = 0;
69 
70  /** Estimate the execution cost of the node (not including the cost of
71  the children. The value returned should be based on the COST_*
72  enumerant values defined in ispc.h. */
73  virtual int EstimateCost() const = 0;
74 
75  /** All AST nodes must track the file position where they are
76  defined. */
78 
79  /** An enumeration for keeping track of the concrete subclass of Value
80  that is actually instantiated.*/
81  enum ASTNodeTy {
82  /* For classes inherited from Expr */
105  /* This is a convenience separator to shorten classof implementations */
107  /* For classes inherited from Stmt */
129  };
130 
131  /** Return an ID for the concrete type of this object. This is used to
132  implement the classof checks. This should not be used for any
133  other purpose, as the values may change as ISPC evolves */
134  unsigned getValueID() const {
135  return SubclassID;
136  }
137 
138  static inline bool classof(ASTNode const*) { return true; }
139 };
140 
141 
142 
143 
144 class AST {
145 public:
146  /** Add the AST for a function described by the given declaration
147  information and source code. */
148  void AddFunction(Symbol *sym, Stmt *code);
149 
150  /** Generate LLVM IR for all of the functions into the current
151  module. */
152  void GenerateIR();
153 
154 private:
155  std::vector<Function *> functions;
156 };
157 
158 
159 /** Callback function type for preorder traversial visiting function for
160  the AST walk.
161  */
162 typedef bool (* ASTPreCallBackFunc)(ASTNode *node, void *data);
163 
164 /** Callback function type for postorder traversial visiting function for
165  the AST walk.
166  */
167 typedef ASTNode * (* ASTPostCallBackFunc)(ASTNode *node, void *data);
168 
169 /** Walk (some portion of) an AST, starting from the given root node. At
170  each node, if preFunc is non-NULL, call it, passing the given void
171  *data pointer; if the call to preFunc function returns false, then the
172  children of the node aren't visited. This function then makes
173  recursive calls to WalkAST() to process the node's children; after
174  doing so, calls postFunc, at the node. The return value from the
175  postFunc call is ignored. */
176 extern ASTNode *WalkAST(ASTNode *root, ASTPreCallBackFunc preFunc,
177  ASTPostCallBackFunc postFunc, void *data);
178 
179 /** Perform simple optimizations on the AST or portion thereof passed to
180  this function, returning the resulting AST. */
181 extern ASTNode *Optimize(ASTNode *root);
182 
183 /** Convenience version of Optimize() for Expr *s that returns an Expr *
184  (rather than an ASTNode *, which would require the caller to cast back
185  to an Expr *). */
186 extern Expr *Optimize(Expr *);
187 
188 /** Convenience version of Optimize() for Expr *s that returns an Stmt *
189  (rather than an ASTNode *, which would require the caller to cast back
190  to a Stmt *). */
191 extern Stmt *Optimize(Stmt *);
192 
193 /** Perform type-checking on the given AST (or portion of one), returning a
194  pointer to the root of the resulting AST. */
195 extern ASTNode *TypeCheck(ASTNode *root);
196 
197 /** Convenience version of TypeCheck() for Expr *s that returns an Expr *. */
198 extern Expr *TypeCheck(Expr *);
199 
200 /** Convenience version of TypeCheck() for Stmt *s that returns an Stmt *. */
201 extern Stmt *TypeCheck(Stmt *);
202 
203 /** Returns an estimate of the execution cost of the tree starting at
204  the given root. */
205 extern int EstimateCost(ASTNode *root);
206 
207 /** Returns true if it would be safe to run the given code with an "all
208  off" mask. */
209 extern bool SafeToRunWithMaskAllOff(ASTNode *root);
210 
211 #endif // ISPC_AST_H
ASTNode(SourcePos p, unsigned scid)
Definition: ast.h:53
Definition: ast.h:144
virtual ASTNode * TypeCheck()=0
Interface class for statements in the ispc language.
Definition: stmt.h:49
virtual int EstimateCost() const =0
const unsigned char SubclassID
Definition: ast.h:51
ASTNode * WalkAST(ASTNode *root, ASTPreCallBackFunc preFunc, ASTPostCallBackFunc postFunc, void *data)
Definition: ast.cpp:74
ASTNode * Optimize(ASTNode *root)
Definition: ast.cpp:282
ASTNode * TypeCheck(ASTNode *root)
Definition: ast.cpp:306
Abstract base class for nodes in the abstract syntax tree (AST).
Definition: ast.h:50
bool SafeToRunWithMaskAllOff(ASTNode *root)
Definition: ast.cpp:506
ASTNode *(* ASTPostCallBackFunc)(ASTNode *node, void *data)
Definition: ast.h:167
virtual ASTNode * Optimize()=0
Representation of a range of positions in a source file.
Definition: ispc.h:134
SourcePos pos
Definition: ast.h:77
unsigned getValueID() const
Definition: ast.h:134
bool(* ASTPreCallBackFunc)(ASTNode *node, void *data)
Definition: ast.h:162
static bool classof(ASTNode const *)
Definition: ast.h:138
std::vector< Function * > functions
Definition: ast.h:155
Representation of a program symbol.
Definition: sym.h:63
void AddFunction(Symbol *sym, Stmt *code)
Definition: ast.cpp:58
void GenerateIR()
Definition: ast.cpp:66
int EstimateCost(ASTNode *root)
Definition: ast.cpp:352
Expr is the abstract base class that defines the interface that all expression types must implement...
Definition: expr.h:48
ASTNodeTy
Definition: ast.h:81
Main ispc.header file. Defines Target, Globals and Opt classes.
virtual ~ASTNode()
Definition: ast.cpp:50