Intel SPMD Program Compiler  1.12.0
expr.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2010-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 expr.h
35  @brief Expr abstract base class and expression implementations
36 */
37 
38 #ifndef ISPC_EXPR_H
39 #define ISPC_EXPR_H 1
40 
41 #include "ast.h"
42 #include "ispc.h"
43 #include "type.h"
44 
45 /** @brief Expr is the abstract base class that defines the interface that
46  all expression types must implement.
47  */
48 class Expr : public ASTNode {
49  public:
50  Expr(SourcePos p, unsigned scid) : ASTNode(p, scid) {}
51 
52  static inline bool classof(Expr const *) { return true; }
53  static inline bool classof(ASTNode const *N) { return N->getValueID() < MaxExprID; }
54 
55  /** This is the main method for Expr implementations to implement. It
56  should call methods in the FunctionEmitContext to emit LLVM IR
57  instructions to the current basic block in order to generate an
58  llvm::Value that represents the expression's value. */
59  virtual llvm::Value *GetValue(FunctionEmitContext *ctx) const = 0;
60 
61  /** For expressions that can provide an lvalue (e.g. array indexing),
62  this function should emit IR that computes the expression's lvalue
63  and returns the corresponding llvm::Value. Expressions that can't
64  provide an lvalue should leave this unimplemented; the default
65  implementation returns NULL. */
66  virtual llvm::Value *GetLValue(FunctionEmitContext *ctx) const;
67 
68  /** Returns the Type of the expression. */
69  virtual const Type *GetType() const = 0;
70 
71  /** Returns the type of the value returned by GetLValueType(); this
72  should be a pointer type of some sort (uniform or varying). */
73  virtual const Type *GetLValueType() const;
74 
75  /** For expressions that have values based on a symbol (e.g. regular
76  symbol references, array indexing, etc.), this returns a pointer to
77  that symbol. */
78  virtual Symbol *GetBaseSymbol() const;
79 
80  /** If this is a constant expression that can be converted to a
81  constant of the given type, this method should return the
82  corresponding llvm::Constant value. Otherwise it should return
83  NULL. */
84  virtual llvm::Constant *GetConstant(const Type *type) const;
85 
86  /** This method should perform early optimizations of the expression
87  (constant folding, etc.) and return a pointer to the resulting
88  expression. If an error is encountered during optimization, NULL
89  should be returned. */
90  virtual Expr *Optimize() = 0;
91 
92  /** This method should perform type checking of the expression and
93  return a pointer to the resulting expression. If an error is
94  encountered, NULL should be returned. */
95  virtual Expr *TypeCheck() = 0;
96 
97  /** Prints the expression to standard output (used for debugging). */
98  virtual void Print() const = 0;
99 };
100 
101 /** @brief Unary expression */
102 class UnaryExpr : public Expr {
103  public:
104  enum Op {
105  PreInc, ///< Pre-increment
106  PreDec, ///< Pre-decrement
107  PostInc, ///< Post-increment
108  PostDec, ///< Post-decrement
109  Negate, ///< Negation
110  LogicalNot, ///< Logical not
111  BitNot, ///< Bit not
112  };
113 
114  UnaryExpr(Op op, Expr *expr, SourcePos pos);
115 
116  static inline bool classof(UnaryExpr const *) { return true; }
117  static inline bool classof(ASTNode const *N) { return N->getValueID() == UnaryExprID; }
118 
119  llvm::Value *GetValue(FunctionEmitContext *ctx) const;
120  const Type *GetType() const;
121  void Print() const;
122  Expr *Optimize();
123  Expr *TypeCheck();
124  int EstimateCost() const;
125 
126  const Op op;
128 };
129 
130 /** @brief Binary expression */
131 class BinaryExpr : public Expr {
132  public:
133  enum Op {
134  Add, ///< Addition
135  Sub, ///< Subtraction
136  Mul, ///< Multiplication
137  Div, ///< Division
138  Mod, ///< Modulus
139  Shl, ///< Shift left
140  Shr, ///< Shift right
141 
142  Lt, ///< Less than
143  Gt, ///< Greater than
144  Le, ///< Less than or equal
145  Ge, ///< Greater than or equal
146  Equal, ///< Equal
147  NotEqual, ///< Not equal
148 
149  BitAnd, ///< Bitwise AND
150  BitXor, ///< Bitwise XOR
151  BitOr, ///< Bitwise OR
152  LogicalAnd, ///< Logical AND
153  LogicalOr, ///< Logical OR
154 
155  Comma, ///< Comma operator
156  };
157 
158  BinaryExpr(Op o, Expr *a, Expr *b, SourcePos p);
159 
160  static inline bool classof(BinaryExpr const *) { return true; }
161  static inline bool classof(ASTNode const *N) { return N->getValueID() == BinaryExprID; }
162 
163  llvm::Value *GetValue(FunctionEmitContext *ctx) const;
164  const Type *GetType() const;
165  const Type *GetLValueType() const;
166  void Print() const;
167 
168  Expr *Optimize();
169  Expr *TypeCheck();
170  int EstimateCost() const;
171 
172  const Op op;
173  Expr *arg0, *arg1;
174 };
175 
176 /** @brief Assignment expression */
177 class AssignExpr : public Expr {
178  public:
179  enum Op {
180  Assign, ///< Regular assignment
181  MulAssign, ///< *= assignment
182  DivAssign, ///< /= assignment
183  ModAssign, ///< %= assignment
184  AddAssign, ///< += assignment
185  SubAssign, ///< -= assignment
186  ShlAssign, ///< <<= assignment
187  ShrAssign, ///< >>= assignment
188  AndAssign, ///< &= assignment
189  XorAssign, ///< ^= assignment
190  OrAssign, ///< |= assignment
191  };
192 
193  AssignExpr(Op o, Expr *a, Expr *b, SourcePos p);
194 
195  static inline bool classof(AssignExpr const *) { return true; }
196  static inline bool classof(ASTNode const *N) { return N->getValueID() == AssignExprID; }
197 
198  llvm::Value *GetValue(FunctionEmitContext *ctx) const;
199  const Type *GetType() const;
200  void Print() const;
201 
202  Expr *Optimize();
203  Expr *TypeCheck();
204  int EstimateCost() const;
205 
206  const Op op;
207  Expr *lvalue, *rvalue;
208 };
209 
210 /** @brief Selection expression, corresponding to "test ? a : b".
211 
212  Returns the value of "a" or "b", depending on the value of "test".
213 */
214 class SelectExpr : public Expr {
215  public:
216  SelectExpr(Expr *test, Expr *a, Expr *b, SourcePos p);
217 
218  static inline bool classof(SelectExpr const *) { return true; }
219  static inline bool classof(ASTNode const *N) { return N->getValueID() == SelectExprID; }
220 
221  llvm::Value *GetValue(FunctionEmitContext *ctx) const;
222  const Type *GetType() const;
223  void Print() const;
224 
225  Expr *Optimize();
226  Expr *TypeCheck();
227  int EstimateCost() const;
228 
229  Expr *test, *expr1, *expr2;
230 };
231 
232 /** @brief A list of expressions.
233 
234  These are mostly used for representing curly-brace delimited
235  initializers for initializers for complex types and for representing
236  the arguments passed to a function call.
237  */
238 class ExprList : public Expr {
239  public:
241  ExprList(Expr *e, SourcePos p) : Expr(p, ExprListID) { exprs.push_back(e); }
242 
243  static inline bool classof(ExprList const *) { return true; }
244  static inline bool classof(ASTNode const *N) { return N->getValueID() == ExprListID; }
245 
246  llvm::Value *GetValue(FunctionEmitContext *ctx) const;
247  const Type *GetType() const;
248  void Print() const;
249  llvm::Constant *GetConstant(const Type *type) const;
250  ExprList *Optimize();
251  ExprList *TypeCheck();
252  int EstimateCost() const;
253 
254  std::vector<Expr *> exprs;
255 };
256 
257 /** @brief Expression representing a function call.
258  */
259 class FunctionCallExpr : public Expr {
260  public:
261  FunctionCallExpr(Expr *func, ExprList *args, SourcePos p, bool isLaunch = false, Expr *launchCountExpr[3] = NULL);
262 
263  static inline bool classof(FunctionCallExpr const *) { return true; }
264  static inline bool classof(ASTNode const *N) { return N->getValueID() == FunctionCallExprID; }
265 
266  llvm::Value *GetValue(FunctionEmitContext *ctx) const;
267  llvm::Value *GetLValue(FunctionEmitContext *ctx) const;
268  const Type *GetType() const;
269  const Type *GetLValueType() const;
270  void Print() const;
271 
272  Expr *Optimize();
273  Expr *TypeCheck();
274  int EstimateCost() const;
275 
278  bool isLaunch;
279  Expr *launchCountExpr[3];
280 };
281 
282 /** @brief Expression representing indexing into something with an integer
283  offset.
284 
285  This is used for both array indexing and indexing into VectorTypes.
286 */
287 class IndexExpr : public Expr {
288  public:
289  IndexExpr(Expr *baseExpr, Expr *index, SourcePos p);
290 
291  static inline bool classof(IndexExpr const *) { return true; }
292  static inline bool classof(ASTNode const *N) { return N->getValueID() == IndexExprID; }
293 
294  llvm::Value *GetValue(FunctionEmitContext *ctx) const;
295  llvm::Value *GetLValue(FunctionEmitContext *ctx) const;
296  const Type *GetType() const;
297  const Type *GetLValueType() const;
298  Symbol *GetBaseSymbol() const;
299  void Print() const;
300 
301  Expr *Optimize();
302  Expr *TypeCheck();
303  int EstimateCost() const;
304 
305  Expr *baseExpr, *index;
306 
307  private:
308  mutable const Type *type;
309  mutable const PointerType *lvalueType;
310 };
311 
312 /** @brief Expression representing member selection ("foo.bar").
313  *
314  * This will also be overloaded to deal with swizzles.
315  */
316 class MemberExpr : public Expr {
317  public:
318  static MemberExpr *create(Expr *expr, const char *identifier, SourcePos pos, SourcePos identifierPos,
319  bool derefLvalue);
320 
321  static inline bool classof(MemberExpr const *) { return true; }
322  static inline bool classof(ASTNode const *N) {
323  return ((N->getValueID() == StructMemberExprID) || (N->getValueID() == VectorMemberExprID));
324  }
325 
326  llvm::Value *GetValue(FunctionEmitContext *ctx) const;
327  llvm::Value *GetLValue(FunctionEmitContext *ctx) const;
328  const Type *GetType() const;
329  Symbol *GetBaseSymbol() const;
330  void Print() const;
331  Expr *Optimize();
332  Expr *TypeCheck();
333  int EstimateCost() const;
334 
335  virtual int getElementNumber() const = 0;
336  virtual const Type *getElementType() const = 0;
337  std::string getCandidateNearMatches() const;
338 
340  std::string identifier;
342 
343  MemberExpr(Expr *expr, const char *identifier, SourcePos pos, SourcePos identifierPos, bool derefLValue,
344  unsigned scid);
345 
346  /** Indicates whether the expression should be dereferenced before the
347  member is found. (i.e. this is true if the MemberExpr was a '->'
348  operator, and is false if it was a '.' operator. */
350 
351  protected:
352  mutable const Type *type, *lvalueType;
353 };
354 
355 /** @brief Expression representing a compile-time constant value.
356 
357  This class can currently represent compile-time constants of anything
358  that is an AtomicType or an EnumType; for anything more complex, we
359  don't currently have a representation of a compile-time constant that
360  can be further reasoned about.
361  */
362 class ConstExpr : public Expr {
363  public:
364  /** Create a ConstExpr from a uniform int8 value */
365  ConstExpr(const Type *t, int8_t i, SourcePos p);
366  /** Create a ConstExpr from a varying int8 value */
367  ConstExpr(const Type *t, int8_t *i, SourcePos p);
368  /** Create a ConstExpr from a uniform uint8 value */
369  ConstExpr(const Type *t, uint8_t u, SourcePos p);
370  /** Create a ConstExpr from a varying uint8 value */
371  ConstExpr(const Type *t, uint8_t *u, SourcePos p);
372 
373  /** Create a ConstExpr from a uniform int16 value */
374  ConstExpr(const Type *t, int16_t i, SourcePos p);
375  /** Create a ConstExpr from a varying int16 value */
376  ConstExpr(const Type *t, int16_t *i, SourcePos p);
377  /** Create a ConstExpr from a uniform uint16 value */
378  ConstExpr(const Type *t, uint16_t u, SourcePos p);
379  /** Create a ConstExpr from a varying uint16 value */
380  ConstExpr(const Type *t, uint16_t *u, SourcePos p);
381 
382  /** Create a ConstExpr from a uniform int32 value */
383  ConstExpr(const Type *t, int32_t i, SourcePos p);
384  /** Create a ConstExpr from a varying int32 value */
385  ConstExpr(const Type *t, int32_t *i, SourcePos p);
386  /** Create a ConstExpr from a uniform uint32 value */
387  ConstExpr(const Type *t, uint32_t u, SourcePos p);
388  /** Create a ConstExpr from a varying uint32 value */
389  ConstExpr(const Type *t, uint32_t *u, SourcePos p);
390 
391  /** Create a ConstExpr from a uniform float value */
392  ConstExpr(const Type *t, float f, SourcePos p);
393  /** Create a ConstExpr from a varying float value */
394  ConstExpr(const Type *t, float *f, SourcePos p);
395 
396  /** Create a ConstExpr from a uniform double value */
397  ConstExpr(const Type *t, double d, SourcePos p);
398  /** Create a ConstExpr from a varying double value */
399  ConstExpr(const Type *t, double *d, SourcePos p);
400 
401  /** Create a ConstExpr from a uniform int64 value */
402  ConstExpr(const Type *t, int64_t i, SourcePos p);
403  /** Create a ConstExpr from a varying int64 value */
404  ConstExpr(const Type *t, int64_t *i, SourcePos p);
405  /** Create a ConstExpr from a uniform uint64 value */
406  ConstExpr(const Type *t, uint64_t i, SourcePos p);
407  /** Create a ConstExpr from a varying uint64 value */
408  ConstExpr(const Type *t, uint64_t *i, SourcePos p);
409 
410  /** Create a ConstExpr from a uniform bool value */
411  ConstExpr(const Type *t, bool b, SourcePos p);
412  /** Create a ConstExpr from a varying bool value */
413  ConstExpr(const Type *t, bool *b, SourcePos p);
414 
415  /** Create a ConstExpr of the same type as the given old ConstExpr,
416  with values given by the "vales" parameter. */
417  ConstExpr(ConstExpr *old, double *values);
418 
419  /** Create ConstExpr with the same type and values as the given one,
420  but at the given position. */
422 
423  static inline bool classof(ConstExpr const *) { return true; }
424  static inline bool classof(ASTNode const *N) { return N->getValueID() == ConstExprID; }
425 
426  llvm::Value *GetValue(FunctionEmitContext *ctx) const;
427  const Type *GetType() const;
428  void Print() const;
429  llvm::Constant *GetConstant(const Type *type) const;
430 
431  Expr *TypeCheck();
432  Expr *Optimize();
433  int EstimateCost() const;
434 
435  /** Return the ConstExpr's values as the given pointer type, doing type
436  conversion from the actual type if needed. If forceVarying is
437  true, then type convert to 'varying' so as to always return a
438  number of values equal to the target vector width into the given
439  pointer. */
440  int GetValues(bool *, bool forceVarying = false) const;
441  int GetValues(int8_t *, bool forceVarying = false) const;
442  int GetValues(uint8_t *, bool forceVarying = false) const;
443  int GetValues(int16_t *, bool forceVarying = false) const;
444  int GetValues(uint16_t *, bool forceVarying = false) const;
445  int GetValues(int32_t *, bool forceVarying = false) const;
446  int GetValues(uint32_t *, bool forceVarying = false) const;
447  int GetValues(float *, bool forceVarying = false) const;
448  int GetValues(int64_t *, bool forceVarying = false) const;
449  int GetValues(uint64_t *, bool forceVarying = false) const;
450  int GetValues(double *, bool forceVarying = false) const;
451 
452  /** Return the number of values in the ConstExpr; should be either 1,
453  if it has uniform type, or the target's vector width if it's
454  varying. */
455  int Count() const;
456 
457  private:
458  AtomicType::BasicType getBasicType() const;
459 
460  const Type *type;
461  union {
462  int8_t int8Val[ISPC_MAX_NVEC];
463  uint8_t uint8Val[ISPC_MAX_NVEC];
464  int16_t int16Val[ISPC_MAX_NVEC];
465  uint16_t uint16Val[ISPC_MAX_NVEC];
466  int32_t int32Val[ISPC_MAX_NVEC];
467  uint32_t uint32Val[ISPC_MAX_NVEC];
468  bool boolVal[ISPC_MAX_NVEC];
469  float floatVal[ISPC_MAX_NVEC];
470  double doubleVal[ISPC_MAX_NVEC];
471  int64_t int64Val[ISPC_MAX_NVEC];
472  uint64_t uint64Val[ISPC_MAX_NVEC];
473  };
474 };
475 
476 /** @brief Expression representing a type cast of the given expression to a
477  probably-different type. */
478 class TypeCastExpr : public Expr {
479  public:
480  TypeCastExpr(const Type *t, Expr *e, SourcePos p);
481 
482  static inline bool classof(TypeCastExpr const *) { return true; }
483  static inline bool classof(ASTNode const *N) { return N->getValueID() == TypeCastExprID; }
484 
485  llvm::Value *GetValue(FunctionEmitContext *ctx) const;
486  llvm::Value *GetLValue(FunctionEmitContext *ctx) const;
487  const Type *GetType() const;
488  const Type *GetLValueType() const;
489  void Print() const;
490  Expr *TypeCheck();
491  Expr *Optimize();
492  int EstimateCost() const;
493  Symbol *GetBaseSymbol() const;
494  llvm::Constant *GetConstant(const Type *type) const;
495 
496  const Type *type;
498 };
499 
500 /** @brief Expression that represents taking a reference of a (non-reference)
501  variable. */
502 class ReferenceExpr : public Expr {
503  public:
505 
506  static inline bool classof(ReferenceExpr const *) { return true; }
507  static inline bool classof(ASTNode const *N) { return N->getValueID() == ReferenceExprID; }
508 
509  llvm::Value *GetValue(FunctionEmitContext *ctx) const;
510  const Type *GetType() const;
511  const Type *GetLValueType() const;
512  Symbol *GetBaseSymbol() const;
513  void Print() const;
514  Expr *TypeCheck();
515  Expr *Optimize();
516  int EstimateCost() const;
517 
519 };
520 
521 /** @brief Common base class that provides shared functionality for
522  PtrDerefExpr and RefDerefExpr. */
523 class DerefExpr : public Expr {
524  public:
525  DerefExpr(Expr *e, SourcePos p, unsigned scid = DerefExprID);
526 
527  static inline bool classof(DerefExpr const *) { return true; }
528  static inline bool classof(ASTNode const *N) {
529  return ((N->getValueID() == DerefExprID) || (N->getValueID() == PtrDerefExprID) ||
530  (N->getValueID() == RefDerefExprID));
531  }
532 
533  llvm::Value *GetValue(FunctionEmitContext *ctx) const;
534  llvm::Value *GetLValue(FunctionEmitContext *ctx) const;
535  const Type *GetLValueType() const;
536  Symbol *GetBaseSymbol() const;
537  Expr *Optimize();
538 
540 };
541 
542 /** @brief Expression that represents dereferencing a pointer to get its
543  value. */
544 class PtrDerefExpr : public DerefExpr {
545  public:
546  PtrDerefExpr(Expr *e, SourcePos p);
547 
548  static inline bool classof(PtrDerefExpr const *) { return true; }
549  static inline bool classof(ASTNode const *N) { return N->getValueID() == PtrDerefExprID; }
550 
551  const Type *GetType() const;
552  void Print() const;
553  Expr *TypeCheck();
554  int EstimateCost() const;
555 };
556 
557 /** @brief Expression that represents dereferencing a reference to get its
558  value. */
559 class RefDerefExpr : public DerefExpr {
560  public:
561  RefDerefExpr(Expr *e, SourcePos p);
562 
563  static inline bool classof(RefDerefExpr const *) { return true; }
564  static inline bool classof(ASTNode const *N) { return N->getValueID() == RefDerefExprID; }
565 
566  const Type *GetType() const;
567  void Print() const;
568  Expr *TypeCheck();
569  int EstimateCost() const;
570 };
571 
572 /** Expression that represents taking the address of an expression. */
573 class AddressOfExpr : public Expr {
574  public:
576 
577  static inline bool classof(AddressOfExpr const *) { return true; }
578  static inline bool classof(ASTNode const *N) { return N->getValueID() == AddressOfExprID; }
579 
580  llvm::Value *GetValue(FunctionEmitContext *ctx) const;
581  const Type *GetType() const;
582  const Type *GetLValueType() const;
583  Symbol *GetBaseSymbol() const;
584  void Print() const;
585  Expr *TypeCheck();
586  Expr *Optimize();
587  int EstimateCost() const;
588  llvm::Constant *GetConstant(const Type *type) const;
589 
591 };
592 
593 /** Expression that returns the size of the given expression or type in
594  bytes. */
595 class SizeOfExpr : public Expr {
596  public:
597  SizeOfExpr(Expr *e, SourcePos p);
598  SizeOfExpr(const Type *t, SourcePos p);
599 
600  static inline bool classof(SizeOfExpr const *) { return true; }
601  static inline bool classof(ASTNode const *N) { return N->getValueID() == SizeOfExprID; }
602 
603  llvm::Value *GetValue(FunctionEmitContext *ctx) const;
604  const Type *GetType() const;
605  void Print() const;
606  Expr *TypeCheck();
607  Expr *Optimize();
608  int EstimateCost() const;
609 
610  /* One of expr or type should be non-NULL (but not both of them). The
611  SizeOfExpr returns the size of whichever one of them isn't NULL. */
613  const Type *type;
614 };
615 
616 /** @brief Expression representing a symbol reference in the program */
617 class SymbolExpr : public Expr {
618  public:
619  SymbolExpr(Symbol *s, SourcePos p);
620 
621  static inline bool classof(SymbolExpr const *) { return true; }
622  static inline bool classof(ASTNode const *N) { return N->getValueID() == SymbolExprID; }
623 
624  llvm::Value *GetValue(FunctionEmitContext *ctx) const;
625  llvm::Value *GetLValue(FunctionEmitContext *ctx) const;
626  const Type *GetType() const;
627  const Type *GetLValueType() const;
628  Symbol *GetBaseSymbol() const;
629  Expr *TypeCheck();
630  Expr *Optimize();
631  void Print() const;
632  int EstimateCost() const;
633 
634  private:
636 };
637 
638 /** @brief Expression representing a function symbol in the program (generally
639  used for a function call).
640  */
641 class FunctionSymbolExpr : public Expr {
642  public:
643  FunctionSymbolExpr(const char *name, const std::vector<Symbol *> &candFuncs, SourcePos pos);
644 
645  static inline bool classof(FunctionSymbolExpr const *) { return true; }
646  static inline bool classof(ASTNode const *N) { return N->getValueID() == FunctionSymbolExprID; }
647 
648  llvm::Value *GetValue(FunctionEmitContext *ctx) const;
649  const Type *GetType() const;
650  Symbol *GetBaseSymbol() const;
651  Expr *TypeCheck();
652  Expr *Optimize();
653  void Print() const;
654  int EstimateCost() const;
655  llvm::Constant *GetConstant(const Type *type) const;
656 
657  /** Given the types of the function arguments, in the presence of
658  function overloading, this method resolves which actual function
659  the arguments match best. If the argCouldBeNULL parameter is
660  non-NULL, each element indicates whether the corresponding argument
661  is the number zero, indicating that it could be a NULL pointer, and
662  if argIsConstant is non-NULL, each element indicates whether the
663  corresponding argument is a compile-time constant value. Both of
664  these parameters may be NULL (for cases where overload resolution
665  is being done just given type information without the parameter
666  argument expressions being available. This function returns true
667  on success.
668  */
669  bool ResolveOverloads(SourcePos argPos, const std::vector<const Type *> &argTypes,
670  const std::vector<bool> *argCouldBeNULL = NULL,
671  const std::vector<bool> *argIsConstant = NULL);
672  Symbol *GetMatchingFunction();
673 
674  private:
675  std::vector<Symbol *> getCandidateFunctions(int argCount) const;
676  static int computeOverloadCost(const FunctionType *ftype, const std::vector<const Type *> &argTypes,
677  const std::vector<bool> *argCouldBeNULL, const std::vector<bool> *argIsConstant,
678  int *cost);
679 
680  /** Name of the function that is being called. */
681  std::string name;
682 
683  /** All of the functions with the name given in the function call;
684  there may be more then one, in which case we need to resolve which
685  overload is the best match. */
686  std::vector<Symbol *> candidateFunctions;
687 
688  /** The actual matching function found after overload resolution. */
690 
692 };
693 
694 /** @brief A sync statement in the program (waits for all launched tasks before
695  proceeding). */
696 class SyncExpr : public Expr {
697  public:
699 
700  static inline bool classof(SyncExpr const *) { return true; }
701  static inline bool classof(ASTNode const *N) { return N->getValueID() == SyncExprID; }
702 
703  llvm::Value *GetValue(FunctionEmitContext *ctx) const;
704  const Type *GetType() const;
705  Expr *TypeCheck();
706  Expr *Optimize();
707  void Print() const;
708  int EstimateCost() const;
709 };
710 
711 /** @brief An expression that represents a NULL pointer. */
712 class NullPointerExpr : public Expr {
713  public:
715 
716  static inline bool classof(NullPointerExpr const *) { return true; }
717  static inline bool classof(ASTNode const *N) { return N->getValueID() == NullPointerExprID; }
718 
719  llvm::Value *GetValue(FunctionEmitContext *ctx) const;
720  const Type *GetType() const;
721  Expr *TypeCheck();
722  Expr *Optimize();
723  llvm::Constant *GetConstant(const Type *type) const;
724  void Print() const;
725  int EstimateCost() const;
726 };
727 
728 /** An expression representing a "new" expression, used for dynamically
729  allocating memory.
730 */
731 class NewExpr : public Expr {
732  public:
733  NewExpr(int typeQual, const Type *type, Expr *initializer, Expr *count, SourcePos tqPos, SourcePos p);
734 
735  static inline bool classof(NewExpr const *) { return true; }
736  static inline bool classof(ASTNode const *N) { return N->getValueID() == NewExprID; }
737 
738  llvm::Value *GetValue(FunctionEmitContext *ctx) const;
739  const Type *GetType() const;
740  Expr *TypeCheck();
741  Expr *Optimize();
742  void Print() const;
743  int EstimateCost() const;
744 
745  /** Type of object to allocate storage for. */
746  const Type *allocType;
747  /** Expression giving the number of elements to allocate, when the
748  "new Foo[expr]" form is used. This may be NULL, in which case a
749  single element of the given type will be allocated. */
751  /** Optional initializer expression used to initialize the allocated
752  memory. */
754  /** Indicates whether this is a "varying new" or "uniform new"
755  (i.e. whether a separate allocation is performed per program
756  instance, or whether a single allocation is performed for the
757  entire gang of program instances.) */
758  bool isVarying;
759 };
760 
761 /** This function indicates whether it's legal to convert from fromType to
762  toType. If the optional errorMsgBase and source position parameters
763  are provided, then an error message is issued if the type conversion
764  isn't possible.
765  */
766 bool CanConvertTypes(const Type *fromType, const Type *toType, const char *errorMsgBase = NULL,
767  SourcePos pos = SourcePos());
768 
769 /** This function attempts to convert the given expression to the given
770  type, returning a pointer to a new expression that is the result. If
771  the required type conversion is illegal, it returns NULL and prints an
772  error message using the provided string to indicate the context for
773  which type conversion was being applied (e.g. "function call
774  parameter").
775  */
776 Expr *TypeConvertExpr(Expr *expr, const Type *toType, const char *errorMsgBase);
777 
779 
780 /** Utility routine that emits code to initialize a symbol given an
781  initializer expression.
782 
783  @param lvalue Memory location of storage for the symbol's data
784  @param symName Name of symbol (used in error messages)
785  @param symType Type of variable being initialized
786  @param initExpr Expression for the initializer
787  @param ctx FunctionEmitContext to use for generating instructions
788  @param pos Source file position of the variable being initialized
789 */
790 void InitSymbol(llvm::Value *lvalue, const Type *symType, Expr *initExpr, FunctionEmitContext *ctx, SourcePos pos);
791 
792 bool PossiblyResolveFunctionOverloads(Expr *expr, const Type *type);
793 
794 #endif // ISPC_EXPR_H
static bool classof(RefDerefExpr const *)
Definition: expr.h:563
static bool classof(DerefExpr const *)
Definition: expr.h:527
static bool classof(SelectExpr const *)
Definition: expr.h:218
Common base class that provides shared functionality for PtrDerefExpr and RefDerefExpr.
Definition: expr.h:523
const Type * type
Definition: expr.h:613
static bool classof(ASTNode const *N)
Definition: expr.h:53
const Type * type
Definition: expr.h:308
virtual int EstimateCost() const =0
static bool classof(BinaryExpr const *)
Definition: expr.h:160
Expr * countExpr
Definition: expr.h:750
|= assignment
Definition: expr.h:190
const Type * type
Definition: expr.h:352
static bool classof(AssignExpr const *)
Definition: expr.h:195
static bool classof(NullPointerExpr const *)
Definition: expr.h:716
Division.
Definition: expr.h:137
/= assignment
Definition: expr.h:182
static bool classof(ASTNode const *N)
Definition: expr.h:196
Bitwise OR.
Definition: expr.h:151
static bool classof(ASTNode const *N)
Definition: expr.h:507
Expression that represents taking a reference of a (non-reference) variable.
Definition: expr.h:502
An expression that represents a NULL pointer.
Definition: expr.h:712
Expr(SourcePos p, unsigned scid)
Definition: expr.h:50
Logical AND.
Definition: expr.h:152
static bool classof(FunctionSymbolExpr const *)
Definition: expr.h:645
static bool classof(SyncExpr const *)
Definition: expr.h:700
NullPointerExpr(SourcePos p)
Definition: expr.h:714
Expression representing a compile-time constant value.
Definition: expr.h:362
static bool classof(ReferenceExpr const *)
Definition: expr.h:506
const PointerType * lvalueType
Definition: expr.h:309
Greater than.
Definition: expr.h:143
Expr * expr
Definition: expr.h:339
static bool classof(UnaryExpr const *)
Definition: expr.h:116
std::string name
Definition: expr.h:681
virtual Expr * Optimize()=0
bool triedToResolve
Definition: expr.h:691
Less than.
Definition: expr.h:142
Symbol * symbol
Definition: expr.h:635
Negation.
Definition: expr.h:109
static bool classof(ASTNode const *N)
Definition: expr.h:578
static bool classof(ExprList const *)
Definition: expr.h:243
const Op op
Definition: expr.h:126
Comma operator.
Definition: expr.h:155
static bool classof(ASTNode const *N)
Definition: expr.h:292
bool CanConvertTypes(const Type *fromType, const Type *toType, const char *errorMsgBase=NULL, SourcePos pos=SourcePos())
Definition: expr.cpp:545
Expr * MakeBinaryExpr(BinaryExpr::Op o, Expr *a, Expr *b, SourcePos p)
Definition: expr.cpp:1617
Symbol * matchingFunc
Definition: expr.h:689
virtual llvm::Constant * GetConstant(const Type *type) const
Definition: expr.cpp:97
A list of expressions.
Definition: expr.h:238
Type implementation for pointers to other types.
Definition: type.h:430
Expr * expr
Definition: expr.h:497
ExprList * args
Definition: expr.h:277
static bool classof(ASTNode const *N)
Definition: expr.h:736
virtual void Print() const =0
static bool classof(ASTNode const *N)
Definition: expr.h:483
static bool classof(ASTNode const *N)
Definition: expr.h:717
virtual llvm::Value * GetValue(FunctionEmitContext *ctx) const =0
Pre-increment.
Definition: expr.h:105
Expr * expr
Definition: expr.h:539
static bool classof(SymbolExpr const *)
Definition: expr.h:621
Modulus.
Definition: expr.h:138
Expr * test
Definition: expr.h:229
std::string identifier
Definition: expr.h:340
Expr * expr
Definition: expr.h:127
static bool classof(MemberExpr const *)
Definition: expr.h:321
static bool classof(ASTNode const *N)
Definition: expr.h:322
Binary expression.
Definition: expr.h:131
static bool classof(SizeOfExpr const *)
Definition: expr.h:600
bool isVarying
Definition: expr.h:758
virtual llvm::Value * GetLValue(FunctionEmitContext *ctx) const
Definition: expr.cpp:86
Definition: expr.h:731
static bool classof(NewExpr const *)
Definition: expr.h:735
Expression representing a type cast of the given expression to a probably-different type...
Definition: expr.h:478
static bool classof(Expr const *)
Definition: expr.h:52
Multiplication.
Definition: expr.h:136
Shift left.
Definition: expr.h:139
Abstract base class for nodes in the abstract syntax tree (AST).
Definition: ast.h:50
static bool classof(TypeCastExpr const *)
Definition: expr.h:482
Expr * func
Definition: expr.h:276
unsigned getValueID() const
Definition: ast.h:134
static bool classof(ASTNode const *N)
Definition: expr.h:264
Expr * arg1
Definition: expr.h:173
Expr * expr
Definition: expr.h:590
static bool classof(ASTNode const *N)
Definition: expr.h:622
virtual const Type * GetLValueType() const
Definition: expr.cpp:91
static bool classof(ASTNode const *N)
Definition: expr.h:646
virtual const Type * GetType() const =0
const Op op
Definition: expr.h:206
%= assignment
Definition: expr.h:183
Expr * expr
Definition: expr.h:518
A sync statement in the program (waits for all launched tasks before proceeding). ...
Definition: expr.h:696
Expr * index
Definition: expr.h:305
Logical not.
Definition: expr.h:110
static bool classof(ASTNode const *N)
Definition: expr.h:161
Representation of a range of positions in a source file.
Definition: ispc.h:131
Expression representing a function symbol in the program (generally used for a function call)...
Definition: expr.h:641
Post-decrement.
Definition: expr.h:108
SyncExpr(SourcePos p)
Definition: expr.h:698
+= assignment
Definition: expr.h:184
static bool classof(PtrDerefExpr const *)
Definition: expr.h:548
const Op op
Definition: expr.h:172
std::vector< Symbol * > candidateFunctions
Definition: expr.h:686
Post-increment.
Definition: expr.h:107
SourcePos pos
Definition: ast.h:77
Not equal.
Definition: expr.h:147
const Type * type
Definition: expr.h:460
void InitSymbol(llvm::Value *lvalue, const Type *symType, Expr *initExpr, FunctionEmitContext *ctx, SourcePos pos)
Definition: expr.cpp:595
Bitwise XOR.
Definition: expr.h:150
Bit not.
Definition: expr.h:111
#define ISPC_MAX_NVEC
Definition: ispc.h:66
static bool classof(ConstExpr const *)
Definition: expr.h:423
static bool classof(ASTNode const *N)
Definition: expr.h:219
const Type * allocType
Definition: expr.h:746
Expression representing member selection ("foo.bar").
Definition: expr.h:316
Pre-decrement.
Definition: expr.h:106
std::vector< Expr * > exprs
Definition: expr.h:254
BasicType
Definition: type.h:314
>>= assignment
Definition: expr.h:187
Unary expression.
Definition: expr.h:102
static bool classof(ASTNode const *N)
Definition: expr.h:244
static bool classof(IndexExpr const *)
Definition: expr.h:291
Expr * rvalue
Definition: expr.h:207
static bool classof(ASTNode const *N)
Definition: expr.h:549
Expression representing indexing into something with an integer offset.
Definition: expr.h:287
Type representing a function (return type + argument types)
Definition: type.h:858
Representation of a program symbol.
Definition: sym.h:63
<<= assignment
Definition: expr.h:186
bool isLaunch
Definition: expr.h:278
Interface class that defines the type abstraction.
Definition: type.h:95
Expression that represents dereferencing a pointer to get its value.
Definition: expr.h:544
Regular assignment.
Definition: expr.h:180
Addition.
Definition: expr.h:134
static bool classof(FunctionCallExpr const *)
Definition: expr.h:263
Less than or equal.
Definition: expr.h:144
&= assignment
Definition: expr.h:188
Expr * TypeConvertExpr(Expr *expr, const Type *toType, const char *errorMsgBase)
Definition: expr.cpp:549
static bool classof(ASTNode const *N)
Definition: expr.h:564
Expr is the abstract base class that defines the interface that all expression types must implement...
Definition: expr.h:48
Assignment expression.
Definition: expr.h:177
-= assignment
Definition: expr.h:185
Expr * expr
Definition: expr.h:612
Subtraction.
Definition: expr.h:135
static bool classof(AddressOfExpr const *)
Definition: expr.h:577
Expression that represents dereferencing a reference to get its value.
Definition: expr.h:559
const Type * type
Definition: expr.h:496
Shift right.
Definition: expr.h:140
virtual Symbol * GetBaseSymbol() const
Definition: expr.cpp:102
Greater than or equal.
Definition: expr.h:145
bool PossiblyResolveFunctionOverloads(Expr *expr, const Type *type)
Definition: expr.cpp:566
*= assignment
Definition: expr.h:181
Equal.
Definition: expr.h:146
Expression representing a symbol reference in the program.
Definition: expr.h:617
bool dereferenceExpr
Definition: expr.h:349
Selection expression, corresponding to "test ? a : b".
Definition: expr.h:214
static bool classof(ASTNode const *N)
Definition: expr.h:528
static bool classof(ASTNode const *N)
Definition: expr.h:601
static bool classof(ASTNode const *N)
Definition: expr.h:424
Main ispc.header file. Defines Target, Globals and Opt classes.
static bool classof(ASTNode const *N)
Definition: expr.h:117
ExprList(Expr *e, SourcePos p)
Definition: expr.h:241
virtual Expr * TypeCheck()=0
const SourcePos identifierPos
Definition: expr.h:341
^= assignment
Definition: expr.h:189
ExprList(SourcePos p)
Definition: expr.h:240
Expr * initExpr
Definition: expr.h:753
Logical OR.
Definition: expr.h:153
static bool classof(ASTNode const *N)
Definition: expr.h:701
Expression representing a function call.
Definition: expr.h:259
File with declarations for classes related to type representation.
Bitwise AND.
Definition: expr.h:149