Intel SPMD Program Compiler  1.12.0
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 { return SubclassID; }
135 
136  static inline bool classof(ASTNode const *) { return true; }
137 };
138 
139 class AST {
140  public:
141  /** Add the AST for a function described by the given declaration
142  information and source code. */
143  void AddFunction(Symbol *sym, Stmt *code);
144 
145  /** Generate LLVM IR for all of the functions into the current
146  module. */
147  void GenerateIR();
148 
149  private:
150  std::vector<Function *> functions;
151 };
152 
153 /** Callback function type for preorder traversial visiting function for
154  the AST walk.
155  */
156 typedef bool (*ASTPreCallBackFunc)(ASTNode *node, void *data);
157 
158 /** Callback function type for postorder traversial visiting function for
159  the AST walk.
160  */
161 typedef ASTNode *(*ASTPostCallBackFunc)(ASTNode *node, void *data);
162 
163 /** Walk (some portion of) an AST, starting from the given root node. At
164  each node, if preFunc is non-NULL, call it, passing the given void
165  *data pointer; if the call to preFunc function returns false, then the
166  children of the node aren't visited. This function then makes
167  recursive calls to WalkAST() to process the node's children; after
168  doing so, calls postFunc, at the node. The return value from the
169  postFunc call is ignored. */
170 extern ASTNode *WalkAST(ASTNode *root, ASTPreCallBackFunc preFunc, ASTPostCallBackFunc postFunc, void *data);
171 
172 /** Perform simple optimizations on the AST or portion thereof passed to
173  this function, returning the resulting AST. */
174 extern ASTNode *Optimize(ASTNode *root);
175 
176 /** Convenience version of Optimize() for Expr *s that returns an Expr *
177  (rather than an ASTNode *, which would require the caller to cast back
178  to an Expr *). */
179 extern Expr *Optimize(Expr *);
180 
181 /** Convenience version of Optimize() for Expr *s that returns an Stmt *
182  (rather than an ASTNode *, which would require the caller to cast back
183  to a Stmt *). */
184 extern Stmt *Optimize(Stmt *);
185 
186 /** Perform type-checking on the given AST (or portion of one), returning a
187  pointer to the root of the resulting AST. */
188 extern ASTNode *TypeCheck(ASTNode *root);
189 
190 /** Convenience version of TypeCheck() for Expr *s that returns an Expr *. */
191 extern Expr *TypeCheck(Expr *);
192 
193 /** Convenience version of TypeCheck() for Stmt *s that returns an Stmt *. */
194 extern Stmt *TypeCheck(Stmt *);
195 
196 /** Returns an estimate of the execution cost of the tree starting at
197  the given root. */
198 extern int EstimateCost(ASTNode *root);
199 
200 /** Returns true if it would be safe to run the given code with an "all
201  off" mask. */
202 extern bool SafeToRunWithMaskAllOff(ASTNode *root);
203 
204 #endif // ISPC_AST_H
virtual int EstimateCost() const =0
ASTNode(SourcePos p, unsigned scid)
Definition: ast.h:53
Definition: ast.h:139
virtual ASTNode * TypeCheck()=0
Interface class for statements in the ispc language.
Definition: stmt.h:49
const unsigned char SubclassID
Definition: ast.h:51
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:50
unsigned getValueID() const
Definition: ast.h:134
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:131
bool(* ASTPreCallBackFunc)(ASTNode *node, void *data)
Definition: ast.h:156
SourcePos pos
Definition: ast.h:77
static bool classof(ASTNode const *)
Definition: ast.h:136
std::vector< Function * > functions
Definition: ast.h:150
Representation of a program symbol.
Definition: sym.h:63
ASTNode *(* ASTPostCallBackFunc)(ASTNode *node, void *data)
Definition: ast.h:161
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