Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ast.h 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. /* -*- c++ -*- */
  2. /*
  3. * Copyright © 2009 Intel Corporation
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice (including the next
  13. * paragraph) shall be included in all copies or substantial portions of the
  14. * Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22. * DEALINGS IN THE SOFTWARE.
  23. */
  24. #pragma once
  25. #ifndef AST_H
  26. #define AST_H
  27. #include "list.h"
  28. #include "glsl_parser_extras.h"
  29. struct ir_instruction;
  30. struct _mesa_glsl_parse_state;
  31. struct YYLTYPE;
  32. class ast_node {
  33. public:
  34. virtual ~ast_node();
  35. virtual void print(void) const;
  36. virtual ir_rvalue *hir(exec_list *instructions,
  37. struct _mesa_glsl_parse_state *state);
  38. /**
  39. * Retrieve the source location of an AST node
  40. *
  41. * This function is primarily used to get the source position of an AST node
  42. * into a form that can be passed to \c _mesa_glsl_error.
  43. *
  44. * \sa _mesa_glsl_error, ast_node::set_location
  45. */
  46. struct YYLTYPE get_location(void) const
  47. {
  48. struct YYLTYPE locp;
  49. locp.source = this->location.source;
  50. locp.first_line = this->location.line;
  51. locp.first_column = this->location.column;
  52. locp.last_line = locp.first_line;
  53. locp.last_column = locp.first_column;
  54. return locp;
  55. }
  56. /**
  57. * Set the source location of an AST node from a parser location
  58. *
  59. * \sa ast_node::get_location
  60. */
  61. void set_location(const struct YYLTYPE &locp)
  62. {
  63. this->location.source = locp.source;
  64. this->location.line = locp.first_line;
  65. this->location.column = locp.first_column;
  66. }
  67. struct {
  68. unsigned source;
  69. unsigned line;
  70. unsigned column;
  71. } location;
  72. exec_node link;
  73. protected:
  74. ast_node(void);
  75. };
  76. enum ast_operators {
  77. ast_assign,
  78. ast_plus, /**< Unary + operator. */
  79. ast_neg,
  80. ast_add,
  81. ast_sub,
  82. ast_mul,
  83. ast_div,
  84. ast_mod,
  85. ast_lshift,
  86. ast_rshift,
  87. ast_less,
  88. ast_greater,
  89. ast_lequal,
  90. ast_gequal,
  91. ast_equal,
  92. ast_nequal,
  93. ast_bit_and,
  94. ast_bit_xor,
  95. ast_bit_or,
  96. ast_bit_not,
  97. ast_logic_and,
  98. ast_logic_xor,
  99. ast_logic_or,
  100. ast_logic_not,
  101. ast_mul_assign,
  102. ast_div_assign,
  103. ast_mod_assign,
  104. ast_add_assign,
  105. ast_sub_assign,
  106. ast_ls_assign,
  107. ast_rs_assign,
  108. ast_and_assign,
  109. ast_xor_assign,
  110. ast_or_assign,
  111. ast_conditional,
  112. ast_pre_inc,
  113. ast_pre_dec,
  114. ast_post_inc,
  115. ast_post_dec,
  116. ast_field_selection,
  117. ast_array_index,
  118. ast_function_call,
  119. ast_identifier,
  120. ast_int_constant,
  121. ast_uint_constant,
  122. ast_float_constant,
  123. ast_bool_constant,
  124. ast_sequence
  125. };
  126. class ast_expression : public ast_node {
  127. public:
  128. ast_expression(int oper, ast_expression *,
  129. ast_expression *, ast_expression *);
  130. ast_expression(const char *identifier) :
  131. oper(ast_identifier)
  132. {
  133. subexpressions[0] = NULL;
  134. subexpressions[1] = NULL;
  135. subexpressions[2] = NULL;
  136. primary_expression.identifier = (char *) identifier;
  137. }
  138. static const char *operator_string(enum ast_operators op);
  139. virtual ir_rvalue *hir(exec_list *instructions,
  140. struct _mesa_glsl_parse_state *state);
  141. virtual void print(void) const;
  142. enum ast_operators oper;
  143. ast_expression *subexpressions[3];
  144. union {
  145. char *identifier;
  146. int int_constant;
  147. float float_constant;
  148. unsigned uint_constant;
  149. int bool_constant;
  150. } primary_expression;
  151. /**
  152. * List of expressions for an \c ast_sequence or parameters for an
  153. * \c ast_function_call
  154. */
  155. exec_list expressions;
  156. };
  157. class ast_expression_bin : public ast_expression {
  158. public:
  159. ast_expression_bin(int oper, ast_expression *, ast_expression *);
  160. virtual void print(void) const;
  161. };
  162. /**
  163. * Subclass of expressions for function calls
  164. */
  165. class ast_function_expression : public ast_expression {
  166. public:
  167. ast_function_expression(ast_expression *callee)
  168. : ast_expression(ast_function_call, callee,
  169. NULL, NULL),
  170. cons(false)
  171. {
  172. /* empty */
  173. }
  174. ast_function_expression(class ast_type_specifier *type)
  175. : ast_expression(ast_function_call, (ast_expression *) type,
  176. NULL, NULL),
  177. cons(true)
  178. {
  179. /* empty */
  180. }
  181. bool is_constructor() const
  182. {
  183. return cons;
  184. }
  185. virtual ir_rvalue *hir(exec_list *instructions,
  186. struct _mesa_glsl_parse_state *state);
  187. private:
  188. /**
  189. * Is this function call actually a constructor?
  190. */
  191. bool cons;
  192. };
  193. /**
  194. * Number of possible operators for an ast_expression
  195. *
  196. * This is done as a define instead of as an additional value in the enum so
  197. * that the compiler won't generate spurious messages like "warning:
  198. * enumeration value ‘ast_num_operators’ not handled in switch"
  199. */
  200. #define AST_NUM_OPERATORS (ast_sequence + 1)
  201. class ast_compound_statement : public ast_node {
  202. public:
  203. ast_compound_statement(int new_scope, ast_node *statements);
  204. virtual void print(void) const;
  205. virtual ir_rvalue *hir(exec_list *instructions,
  206. struct _mesa_glsl_parse_state *state);
  207. int new_scope;
  208. exec_list statements;
  209. };
  210. class ast_declaration : public ast_node {
  211. public:
  212. ast_declaration(char *identifier, int is_array, ast_expression *array_size,
  213. ast_expression *initializer);
  214. virtual void print(void) const;
  215. char *identifier;
  216. int is_array;
  217. ast_expression *array_size;
  218. ast_expression *initializer;
  219. };
  220. enum {
  221. ast_precision_high = 0, /**< Default precision. */
  222. ast_precision_medium,
  223. ast_precision_low
  224. };
  225. struct ast_type_qualifier {
  226. unsigned invariant:1;
  227. unsigned constant:1;
  228. unsigned attribute:1;
  229. unsigned varying:1;
  230. unsigned in:1;
  231. unsigned out:1;
  232. unsigned centroid:1;
  233. unsigned uniform:1;
  234. unsigned smooth:1;
  235. unsigned flat:1;
  236. unsigned noperspective:1;
  237. };
  238. class ast_struct_specifier : public ast_node {
  239. public:
  240. ast_struct_specifier(char *identifier, ast_node *declarator_list);
  241. virtual void print(void) const;
  242. virtual ir_rvalue *hir(exec_list *instructions,
  243. struct _mesa_glsl_parse_state *state);
  244. char *name;
  245. exec_list declarations;
  246. };
  247. enum ast_types {
  248. ast_void,
  249. ast_float,
  250. ast_int,
  251. ast_uint,
  252. ast_bool,
  253. ast_vec2,
  254. ast_vec3,
  255. ast_vec4,
  256. ast_bvec2,
  257. ast_bvec3,
  258. ast_bvec4,
  259. ast_ivec2,
  260. ast_ivec3,
  261. ast_ivec4,
  262. ast_uvec2,
  263. ast_uvec3,
  264. ast_uvec4,
  265. ast_mat2,
  266. ast_mat2x3,
  267. ast_mat2x4,
  268. ast_mat3x2,
  269. ast_mat3,
  270. ast_mat3x4,
  271. ast_mat4x2,
  272. ast_mat4x3,
  273. ast_mat4,
  274. ast_sampler1d,
  275. ast_sampler2d,
  276. ast_sampler2drect,
  277. ast_sampler3d,
  278. ast_samplercube,
  279. ast_sampler1dshadow,
  280. ast_sampler2dshadow,
  281. ast_sampler2drectshadow,
  282. ast_samplercubeshadow,
  283. ast_sampler1darray,
  284. ast_sampler2darray,
  285. ast_sampler1darrayshadow,
  286. ast_sampler2darrayshadow,
  287. ast_isampler1d,
  288. ast_isampler2d,
  289. ast_isampler3d,
  290. ast_isamplercube,
  291. ast_isampler1darray,
  292. ast_isampler2darray,
  293. ast_usampler1d,
  294. ast_usampler2d,
  295. ast_usampler3d,
  296. ast_usamplercube,
  297. ast_usampler1darray,
  298. ast_usampler2darray,
  299. ast_struct,
  300. ast_type_name
  301. };
  302. class ast_type_specifier : public ast_node {
  303. public:
  304. ast_type_specifier(int specifier);
  305. /** Construct a type specifier from a type name */
  306. ast_type_specifier(const char *name)
  307. : type_specifier(ast_type_name), type_name(name), structure(NULL),
  308. is_array(false), array_size(NULL), precision(ast_precision_high)
  309. {
  310. /* empty */
  311. }
  312. /** Construct a type specifier from a structure definition */
  313. ast_type_specifier(ast_struct_specifier *s)
  314. : type_specifier(ast_struct), type_name(s->name), structure(s),
  315. is_array(false), array_size(NULL), precision(ast_precision_high)
  316. {
  317. /* empty */
  318. }
  319. const struct glsl_type *glsl_type(const char **name,
  320. struct _mesa_glsl_parse_state *state)
  321. const;
  322. virtual void print(void) const;
  323. ir_rvalue *hir(exec_list *, struct _mesa_glsl_parse_state *);
  324. enum ast_types type_specifier;
  325. const char *type_name;
  326. ast_struct_specifier *structure;
  327. int is_array;
  328. ast_expression *array_size;
  329. unsigned precision:2;
  330. };
  331. class ast_fully_specified_type : public ast_node {
  332. public:
  333. virtual void print(void) const;
  334. ast_type_qualifier qualifier;
  335. ast_type_specifier *specifier;
  336. };
  337. class ast_declarator_list : public ast_node {
  338. public:
  339. ast_declarator_list(ast_fully_specified_type *);
  340. virtual void print(void) const;
  341. virtual ir_rvalue *hir(exec_list *instructions,
  342. struct _mesa_glsl_parse_state *state);
  343. ast_fully_specified_type *type;
  344. exec_list declarations;
  345. /**
  346. * Special flag for vertex shader "invariant" declarations.
  347. *
  348. * Vertex shaders can contain "invariant" variable redeclarations that do
  349. * not include a type. For example, "invariant gl_Position;". This flag
  350. * is used to note these cases when no type is specified.
  351. */
  352. int invariant;
  353. };
  354. class ast_parameter_declarator : public ast_node {
  355. public:
  356. virtual void print(void) const;
  357. virtual ir_rvalue *hir(exec_list *instructions,
  358. struct _mesa_glsl_parse_state *state);
  359. ast_fully_specified_type *type;
  360. char *identifier;
  361. int is_array;
  362. ast_expression *array_size;
  363. static void parameters_to_hir(exec_list *ast_parameters,
  364. bool formal, exec_list *ir_parameters,
  365. struct _mesa_glsl_parse_state *state);
  366. private:
  367. /** Is this parameter declaration part of a formal parameter list? */
  368. bool formal_parameter;
  369. /**
  370. * Is this parameter 'void' type?
  371. *
  372. * This field is set by \c ::hir.
  373. */
  374. bool is_void;
  375. };
  376. class ast_function : public ast_node {
  377. public:
  378. ast_function(void);
  379. virtual void print(void) const;
  380. virtual ir_rvalue *hir(exec_list *instructions,
  381. struct _mesa_glsl_parse_state *state);
  382. ast_fully_specified_type *return_type;
  383. char *identifier;
  384. exec_list parameters;
  385. private:
  386. /**
  387. * Is this prototype part of the function definition?
  388. *
  389. * Used by ast_function_definition::hir to process the parameters, etc.
  390. * of the function.
  391. *
  392. * \sa ::hir
  393. */
  394. bool is_definition;
  395. /**
  396. * Function signature corresponding to this function prototype instance
  397. *
  398. * Used by ast_function_definition::hir to process the parameters, etc.
  399. * of the function.
  400. *
  401. * \sa ::hir
  402. */
  403. class ir_function_signature *signature;
  404. friend class ast_function_definition;
  405. };
  406. class ast_declaration_statement : public ast_node {
  407. public:
  408. ast_declaration_statement(void);
  409. enum {
  410. ast_function,
  411. ast_declaration,
  412. ast_precision
  413. } mode;
  414. union {
  415. class ast_function *function;
  416. ast_declarator_list *declarator;
  417. ast_type_specifier *type;
  418. ast_node *node;
  419. } declaration;
  420. };
  421. class ast_expression_statement : public ast_node {
  422. public:
  423. ast_expression_statement(ast_expression *);
  424. virtual void print(void) const;
  425. virtual ir_rvalue *hir(exec_list *instructions,
  426. struct _mesa_glsl_parse_state *state);
  427. ast_expression *expression;
  428. };
  429. class ast_case_label : public ast_node {
  430. public:
  431. /**
  432. * An expression of NULL means 'default'.
  433. */
  434. ast_expression *expression;
  435. };
  436. class ast_selection_statement : public ast_node {
  437. public:
  438. ast_selection_statement(ast_expression *condition,
  439. ast_node *then_statement,
  440. ast_node *else_statement);
  441. virtual void print(void) const;
  442. virtual ir_rvalue *hir(exec_list *instructions,
  443. struct _mesa_glsl_parse_state *state);
  444. ast_expression *condition;
  445. ast_node *then_statement;
  446. ast_node *else_statement;
  447. };
  448. class ast_switch_statement : public ast_node {
  449. public:
  450. ast_expression *expression;
  451. exec_list statements;
  452. };
  453. class ast_iteration_statement : public ast_node {
  454. public:
  455. ast_iteration_statement(int mode, ast_node *init, ast_node *condition,
  456. ast_expression *rest_expression, ast_node *body);
  457. virtual void print(void) const;
  458. virtual ir_rvalue *hir(exec_list *, struct _mesa_glsl_parse_state *);
  459. enum ast_iteration_modes {
  460. ast_for,
  461. ast_while,
  462. ast_do_while
  463. } mode;
  464. ast_node *init_statement;
  465. ast_node *condition;
  466. ast_expression *rest_expression;
  467. ast_node *body;
  468. private:
  469. /**
  470. * Generate IR from the condition of a loop
  471. *
  472. * This is factored out of ::hir because some loops have the condition
  473. * test at the top (for and while), and others have it at the end (do-while).
  474. */
  475. void condition_to_hir(class ir_loop *, struct _mesa_glsl_parse_state *);
  476. };
  477. class ast_jump_statement : public ast_node {
  478. public:
  479. ast_jump_statement(int mode, ast_expression *return_value);
  480. virtual void print(void) const;
  481. virtual ir_rvalue *hir(exec_list *instructions,
  482. struct _mesa_glsl_parse_state *state);
  483. enum ast_jump_modes {
  484. ast_continue,
  485. ast_break,
  486. ast_return,
  487. ast_discard
  488. } mode;
  489. ast_expression *opt_return_value;
  490. };
  491. class ast_function_definition : public ast_node {
  492. public:
  493. virtual void print(void) const;
  494. virtual ir_rvalue *hir(exec_list *instructions,
  495. struct _mesa_glsl_parse_state *state);
  496. ast_function *prototype;
  497. ast_compound_statement *body;
  498. };
  499. extern void
  500. _mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state);
  501. extern struct ir_rvalue *
  502. _mesa_ast_field_selection_to_hir(const struct ast_expression *expr,
  503. exec_list *instructions,
  504. struct _mesa_glsl_parse_state *state);
  505. #endif /* AST_H */