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