1848b8605Smrg/*
2848b8605Smrg * Copyright © 2009 Intel Corporation
3848b8605Smrg *
4848b8605Smrg * Permission is hereby granted, free of charge, to any person obtaining a
5848b8605Smrg * copy of this software and associated documentation files (the "Software"),
6848b8605Smrg * to deal in the Software without restriction, including without limitation
7848b8605Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8848b8605Smrg * and/or sell copies of the Software, and to permit persons to whom the
9848b8605Smrg * Software is furnished to do so, subject to the following conditions:
10848b8605Smrg *
11848b8605Smrg * The above copyright notice and this permission notice (including the next
12848b8605Smrg * paragraph) shall be included in all copies or substantial portions of the
13848b8605Smrg * Software.
14848b8605Smrg *
15848b8605Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16848b8605Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17848b8605Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18848b8605Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19848b8605Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20848b8605Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21848b8605Smrg * DEALINGS IN THE SOFTWARE.
22848b8605Smrg */
23b8e80941Smrg
24b8e80941Smrg#ifndef PROGRAM_PARSER_H
25b8e80941Smrg#define PROGRAM_PARSER_H
26848b8605Smrg
27848b8605Smrg#include "main/config.h"
28848b8605Smrg#include "program/prog_parameter.h"
29848b8605Smrg
30848b8605Smrgstruct gl_context;
31848b8605Smrg
32848b8605Smrgenum asm_type {
33848b8605Smrg   at_none,
34848b8605Smrg   at_address,
35848b8605Smrg   at_attrib,
36848b8605Smrg   at_param,
37848b8605Smrg   at_temp,
38848b8605Smrg   at_output
39848b8605Smrg};
40848b8605Smrg
41848b8605Smrgstruct asm_symbol {
42848b8605Smrg   struct asm_symbol *next;    /**< List linkage for freeing. */
43848b8605Smrg   const char *name;
44848b8605Smrg   enum asm_type type;
45848b8605Smrg   unsigned attrib_binding;
46848b8605Smrg   unsigned output_binding;   /**< Output / result register number. */
47848b8605Smrg
48848b8605Smrg   /**
49848b8605Smrg    * One of PROGRAM_STATE_VAR or PROGRAM_CONSTANT.
50848b8605Smrg    */
51848b8605Smrg   unsigned param_binding_type;
52848b8605Smrg
53848b8605Smrg   /**
54848b8605Smrg    * Offset into the program_parameter_list where the tokens representing our
55848b8605Smrg    * bound state (or constants) start.
56848b8605Smrg    */
57848b8605Smrg   unsigned param_binding_begin;
58848b8605Smrg
59848b8605Smrg   /**
60848b8605Smrg    * Constants put into the parameter list may be swizzled.  This
61848b8605Smrg    * field contain's the symbol's swizzle. (SWIZZLE_X/Y/Z/W)
62848b8605Smrg    */
63848b8605Smrg   unsigned param_binding_swizzle;
64848b8605Smrg
65848b8605Smrg   /* This is how many entries in the program_parameter_list we take up
66848b8605Smrg    * with our state tokens or constants. Note that this is _not_ the same as
67848b8605Smrg    * the number of param registers we eventually use.
68848b8605Smrg    */
69848b8605Smrg   unsigned param_binding_length;
70848b8605Smrg
71848b8605Smrg   /**
72848b8605Smrg    * Index of the temp register assigned to this variable.
73848b8605Smrg    */
74848b8605Smrg   unsigned temp_binding;
75848b8605Smrg
76848b8605Smrg   /**
77848b8605Smrg    * Flag whether or not a PARAM is an array
78848b8605Smrg    */
79848b8605Smrg   unsigned param_is_array:1;
80848b8605Smrg
81848b8605Smrg
82848b8605Smrg   /**
83848b8605Smrg    * Flag whether or not a PARAM array is accessed indirectly
84848b8605Smrg    */
85848b8605Smrg   unsigned param_accessed_indirectly:1;
86848b8605Smrg
87848b8605Smrg
88848b8605Smrg   /**
89848b8605Smrg    * \brief Is first pass of parameter layout done with this variable?
90848b8605Smrg    *
91848b8605Smrg    * The parameter layout routine operates in two passes.  This flag tracks
92848b8605Smrg    * whether or not the first pass has handled this variable.
93848b8605Smrg    *
94848b8605Smrg    * \sa _mesa_layout_parameters
95848b8605Smrg    */
96848b8605Smrg   unsigned pass1_done:1;
97848b8605Smrg};
98848b8605Smrg
99848b8605Smrg
100848b8605Smrgstruct asm_vector {
101848b8605Smrg   unsigned count;
102848b8605Smrg   gl_constant_value data[4];
103848b8605Smrg};
104848b8605Smrg
105848b8605Smrg
106848b8605Smrgstruct asm_swizzle_mask {
107848b8605Smrg   unsigned swizzle:12;
108848b8605Smrg   unsigned mask:4;
109848b8605Smrg};
110848b8605Smrg
111848b8605Smrg
112848b8605Smrgstruct asm_src_register {
113848b8605Smrg   struct prog_src_register Base;
114848b8605Smrg
115848b8605Smrg   /**
116848b8605Smrg    * Symbol associated with indirect access to parameter arrays.
117848b8605Smrg    *
118848b8605Smrg    * If \c Base::RelAddr is 1, this will point to the symbol for the parameter
119848b8605Smrg    * that is being dereferenced.  Further, \c Base::Index will be the offset
120848b8605Smrg    * from the address register being used.
121848b8605Smrg    */
122848b8605Smrg   struct asm_symbol *Symbol;
123848b8605Smrg};
124848b8605Smrg
125848b8605Smrg
126848b8605Smrgstruct asm_instruction {
127848b8605Smrg   struct prog_instruction Base;
128848b8605Smrg   struct asm_instruction *next;
129848b8605Smrg   struct asm_src_register SrcReg[3];
130848b8605Smrg};
131848b8605Smrg
132848b8605Smrg
133848b8605Smrgstruct asm_parser_state {
134848b8605Smrg   struct gl_context *ctx;
135848b8605Smrg   struct gl_program *prog;
136848b8605Smrg
137b8e80941Smrg   /** Memory context to attach instructions to. */
138b8e80941Smrg   void *mem_ctx;
139b8e80941Smrg
140848b8605Smrg   /**
141848b8605Smrg    * Per-program target limits
142848b8605Smrg    */
143848b8605Smrg   struct gl_program_constants *limits;
144848b8605Smrg
145848b8605Smrg   struct _mesa_symbol_table *st;
146848b8605Smrg
147848b8605Smrg   /**
148848b8605Smrg    * Linked list of symbols
149848b8605Smrg    *
150848b8605Smrg    * This list is \b only used when cleaning up compiler state and freeing
151848b8605Smrg    * memory.
152848b8605Smrg    */
153848b8605Smrg   struct asm_symbol *sym;
154848b8605Smrg
155848b8605Smrg   /**
156848b8605Smrg    * State for the lexer.
157848b8605Smrg    */
158848b8605Smrg   void *scanner;
159848b8605Smrg
160848b8605Smrg   /**
161848b8605Smrg    * Linked list of instructions generated during parsing.
162848b8605Smrg    */
163848b8605Smrg   /*@{*/
164848b8605Smrg   struct asm_instruction *inst_head;
165848b8605Smrg   struct asm_instruction *inst_tail;
166848b8605Smrg   /*@}*/
167848b8605Smrg
168848b8605Smrg
169848b8605Smrg   /**
170848b8605Smrg    * Selected limits copied from gl_constants
171848b8605Smrg    *
172848b8605Smrg    * These are limits from the GL context, but various bits in the program
173848b8605Smrg    * must be validated against these values.
174848b8605Smrg    */
175848b8605Smrg   /*@{*/
176848b8605Smrg   unsigned MaxTextureCoordUnits;
177848b8605Smrg   unsigned MaxTextureImageUnits;
178848b8605Smrg   unsigned MaxTextureUnits;
179848b8605Smrg   unsigned MaxClipPlanes;
180848b8605Smrg   unsigned MaxLights;
181848b8605Smrg   unsigned MaxProgramMatrices;
182848b8605Smrg   unsigned MaxDrawBuffers;
183848b8605Smrg   /*@}*/
184848b8605Smrg
185848b8605Smrg   /**
186848b8605Smrg    * Value to use in state vector accessors for environment and local
187848b8605Smrg    * parameters
188848b8605Smrg    */
189848b8605Smrg   unsigned state_param_enum;
190848b8605Smrg
191848b8605Smrg
192848b8605Smrg   /**
193848b8605Smrg    * Input attributes bound to specific names
194848b8605Smrg    *
195848b8605Smrg    * This is only needed so that errors can be properly produced when
196848b8605Smrg    * multiple ATTRIB statements bind illegal combinations of vertex
197848b8605Smrg    * attributes.
198848b8605Smrg    */
199848b8605Smrg   GLbitfield64 InputsBound;
200848b8605Smrg
201848b8605Smrg   enum {
202848b8605Smrg      invalid_mode = 0,
203848b8605Smrg      ARB_vertex,
204848b8605Smrg      ARB_fragment
205848b8605Smrg   } mode;
206848b8605Smrg
207848b8605Smrg   struct {
208848b8605Smrg      unsigned PositionInvariant:1;
209848b8605Smrg      unsigned Fog:2;
210848b8605Smrg      unsigned PrecisionHint:2;
211848b8605Smrg      unsigned DrawBuffers:1;
212848b8605Smrg      unsigned Shadow:1;
213848b8605Smrg      unsigned TexRect:1;
214848b8605Smrg      unsigned TexArray:1;
215848b8605Smrg      unsigned OriginUpperLeft:1;
216848b8605Smrg      unsigned PixelCenterInteger:1;
217848b8605Smrg   } option;
218848b8605Smrg
219848b8605Smrg   struct {
220848b8605Smrg      unsigned UsesKill:1;
221848b8605Smrg   } fragment;
222848b8605Smrg};
223848b8605Smrg
224848b8605Smrg#define OPTION_NONE        0
225848b8605Smrg#define OPTION_FOG_EXP     1
226848b8605Smrg#define OPTION_FOG_EXP2    2
227848b8605Smrg#define OPTION_FOG_LINEAR  3
228848b8605Smrg#define OPTION_NICEST      1
229848b8605Smrg#define OPTION_FASTEST     2
230848b8605Smrg
231848b8605Smrgtypedef struct YYLTYPE {
232848b8605Smrg   int first_line;
233848b8605Smrg   int first_column;
234848b8605Smrg   int last_line;
235848b8605Smrg   int last_column;
236848b8605Smrg   int position;
237848b8605Smrg} YYLTYPE;
238848b8605Smrg
239848b8605Smrg#define YYLTYPE_IS_DECLARED 1
240848b8605Smrg#define YYLTYPE_IS_TRIVIAL 1
241848b8605Smrg
242848b8605Smrg
243848b8605Smrgextern GLboolean _mesa_parse_arb_program(struct gl_context *ctx, GLenum target,
244848b8605Smrg    const GLubyte *str, GLsizei len, struct asm_parser_state *state);
245848b8605Smrg
246848b8605Smrg
247848b8605Smrg
248848b8605Smrg/* From program_lexer.l. */
249848b8605Smrgextern void _mesa_program_lexer_dtor(void *scanner);
250848b8605Smrg
251848b8605Smrgextern void _mesa_program_lexer_ctor(void **scanner,
252848b8605Smrg    struct asm_parser_state *state, const char *string, size_t len);
253848b8605Smrg
254848b8605Smrg
255848b8605Smrg/**
256848b8605Smrg *\name From program_parse_extra.c
257848b8605Smrg */
258848b8605Smrg/*@{*/
259848b8605Smrg
260848b8605Smrg/**
261848b8605Smrg * Parses and processes an option string to an ARB vertex program
262848b8605Smrg *
263848b8605Smrg * \return
264848b8605Smrg * Non-zero on success, zero on failure.
265848b8605Smrg */
266848b8605Smrgextern int _mesa_ARBvp_parse_option(struct asm_parser_state *state,
267848b8605Smrg    const char *option);
268848b8605Smrg
269848b8605Smrg/**
270848b8605Smrg * Parses and processes an option string to an ARB fragment program
271848b8605Smrg *
272848b8605Smrg * \return
273848b8605Smrg * Non-zero on success, zero on failure.
274848b8605Smrg */
275848b8605Smrgextern int _mesa_ARBfp_parse_option(struct asm_parser_state *state,
276848b8605Smrg    const char *option);
277848b8605Smrg
278848b8605Smrg/**
279848b8605Smrg * Parses and processes instruction suffixes
280848b8605Smrg *
281848b8605Smrg * Instruction suffixes, such as \c _SAT, are processed.  The relevant bits
282848b8605Smrg * are set in \c inst.  If suffixes are encountered that are either not known
283848b8605Smrg * or not supported by the modes and options set in \c state, zero will be
284848b8605Smrg * returned.
285848b8605Smrg *
286848b8605Smrg * \return
287848b8605Smrg * Non-zero on success, zero on failure.
288848b8605Smrg */
289848b8605Smrgextern int _mesa_parse_instruction_suffix(const struct asm_parser_state *state,
290848b8605Smrg    const char *suffix, struct prog_instruction *inst);
291848b8605Smrg
292848b8605Smrg/*@}*/
293b8e80941Smrg
294b8e80941Smrg#endif /* PROGRAM_PARSER_H */
295