aboutsummaryrefslogtreecommitdiff
path: root/nx-X11/extras/Mesa/src/mesa/shader/slang/slang_assemble_typeinfo.c
blob: ce78f66ebe2b7754f117a40612ca7eef7d7615fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/*
 * Mesa 3-D graphics library
 * Version:  6.3
 *
 * Copyright (C) 2005  Brian Paul   All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/**
 * \file slang_assemble_typeinfo.c
 * slang type info
 * \author Michal Krol
 */

#include "imports.h"
#include "slang_utility.h"
#include "slang_assemble_typeinfo.h"

/* slang_assembly_typeinfo */

void slang_assembly_typeinfo_construct (slang_assembly_typeinfo *ti)
{
	slang_type_specifier_construct (&ti->spec);
}

void slang_assembly_typeinfo_destruct (slang_assembly_typeinfo *ti)
{
	slang_type_specifier_destruct (&ti->spec);
}

/* _slang_typeof_operation() */

int _slang_typeof_operation (slang_operation *op, slang_assembly_name_space *space,
	slang_assembly_typeinfo *ti)
{
	ti->can_be_referenced = 0;
	ti->is_swizzled = 0;

	switch (op->type)
	{
	case slang_oper_block_no_new_scope:
	case slang_oper_block_new_scope:
	case slang_oper_variable_decl:
	case slang_oper_asm:
	case slang_oper_break:
	case slang_oper_continue:
	case slang_oper_discard:
	case slang_oper_return:
	case slang_oper_if:
	case slang_oper_while:
	case slang_oper_do:
	case slang_oper_for:
	case slang_oper_void:
		ti->spec.type = slang_spec_void;
		break;
	case slang_oper_expression:
	case slang_oper_assign:
	case slang_oper_addassign:
	case slang_oper_subassign:
	case slang_oper_mulassign:
	case slang_oper_divassign:
	case slang_oper_preincrement:
	case slang_oper_predecrement:
		if (!_slang_typeof_operation (op->children, space, ti))
			return 0;
		break;
	case slang_oper_literal_bool:
	case slang_oper_logicalor:
	case slang_oper_logicalxor:
	case slang_oper_logicaland:
	case slang_oper_equal:
	case slang_oper_notequal:
	case slang_oper_less:
	case slang_oper_greater:
	case slang_oper_lessequal:
	case slang_oper_greaterequal:
	case slang_oper_not:
		ti->spec.type = slang_spec_bool;
		break;
	case slang_oper_literal_int:
		ti->spec.type = slang_spec_int;
		break;
	case slang_oper_literal_float:
		ti->spec.type = slang_spec_float;
		break;
	case slang_oper_identifier:
		{
			slang_variable *var;

			var = _slang_locate_variable (op->locals, op->identifier, 1);
			if (var == NULL)
				return 0;
			if (!slang_type_specifier_copy (&ti->spec, &var->type.specifier))
				return 0;
			ti->can_be_referenced = 1;
		}
		break;
	case slang_oper_sequence:
		/* TODO: check [0] and [1] if they match */
		if (!_slang_typeof_operation (op->children + 1, space, ti))
			return 0;
		ti->can_be_referenced = 0;
		ti->is_swizzled = 0;
		break;
	/*case slang_oper_modassign:*/
	/*case slang_oper_lshassign:*/
	/*case slang_oper_rshassign:*/
	/*case slang_oper_orassign:*/
	/*case slang_oper_xorassign:*/
	/*case slang_oper_andassign:*/
	case slang_oper_select:
		/* TODO: check [1] and [2] if they match */
		if (!_slang_typeof_operation (op->children + 1, space, ti))
			return 0;
		ti->can_be_referenced = 0;
		ti->is_swizzled = 0;
		break;
	/*case slang_oper_bitor:*/
	/*case slang_oper_bitxor:*/
	/*case slang_oper_bitand:*/
	/*case slang_oper_lshift:*/
	/*case slang_oper_rshift:*/
	case slang_oper_add:
		{
			int exists;
			if (!_slang_typeof_function ("+", op->children, 2, space, &ti->spec, &exists))
				return 0;
			if (!exists)
				return 0;
		}
		break;
	case slang_oper_subtract:
		{
			int exists;
			if (!_slang_typeof_function ("-", op->children, 2, space, &ti->spec, &exists))
				return 0;
			if (!exists)
				return 0;
		}
		break;
	case slang_oper_multiply:
		{
			int exists;
			if (!_slang_typeof_function ("*", op->children, 2, space, &ti->spec, &exists))
				return 0;
			if (!exists)
				return 0;
		}
		break;
	case slang_oper_divide:
		{
			int exists;
			if (!_slang_typeof_function ("/", op->children, 2, space, &ti->spec, &exists))
				return 0;
			if (!exists)
				return 0;
		}
		break;
	/*case slang_oper_modulus:*/
	case slang_oper_plus:
		{
			int exists;
			if (!_slang_typeof_function ("+", op->children, 1, space, &ti->spec, &exists))
				return 0;
			if (!exists)
				return 0;
		}
		break;
	case slang_oper_minus:
		{
			int exists;
			if (!_slang_typeof_function ("-", op->children, 1, space, &ti->spec, &exists))
				return 0;
			if (!exists)
				return 0;
		}
		break;
	/*case slang_oper_complement:*/
	case slang_oper_subscript:
		{
			slang_assembly_typeinfo _ti;
			slang_assembly_typeinfo_construct (&_ti);
			if (!_slang_typeof_operation (op->children, space, &_ti))
			{
				slang_assembly_typeinfo_destruct (&_ti);
				return 0;
			}
			ti->can_be_referenced = _ti.can_be_referenced;
			switch (_ti.spec.type)
			{
			case slang_spec_bvec2:
			case slang_spec_bvec3:
			case slang_spec_bvec4:
				ti->spec.type = slang_spec_bool;
				break;
			case slang_spec_ivec2:
			case slang_spec_ivec3:
			case slang_spec_ivec4:
				ti->spec.type = slang_spec_int;
				break;
			case slang_spec_vec2:
			case slang_spec_vec3:
			case slang_spec_vec4:
				ti->spec.type = slang_spec_float;
				break;
			case slang_spec_mat2:
				ti->spec.type = slang_spec_vec2;
				break;
			case slang_spec_mat3:
				ti->spec.type = slang_spec_vec3;
				break;
			case slang_spec_mat4:
				ti->spec.type = slang_spec_vec4;
				break;
			case slang_spec_array:
				if (!slang_type_specifier_copy (&ti->spec, _ti.spec._array))
				{
					slang_assembly_typeinfo_destruct (&_ti);
					return 0;
				}
				break;
			default:
				slang_assembly_typeinfo_destruct (&_ti);
				return 0;
			}
			slang_assembly_typeinfo_destruct (&_ti);
		}
		break;
	case slang_oper_call:
		{
			int exists;
			if (!_slang_typeof_function (op->identifier, op->children, op->num_children, space,
				&ti->spec, &exists))
				return 0;
			if (!exists)
			{
				slang_struct *s = slang_struct_scope_find (space->structs, op->identifier, 1);
				if (s != NULL)
				{
					ti->spec.type = slang_spec_struct;
					ti->spec._struct = (slang_struct *) slang_alloc_malloc (sizeof (slang_struct));
					if (ti->spec._struct == NULL)
						return 0;
					if (!slang_struct_construct_a (ti->spec._struct))
					{
						slang_alloc_free (ti->spec._struct);
						ti->spec._struct = NULL;
						return 0;
					}
					if (!slang_struct_copy (ti->spec._struct, s))
						return 0;
				}
				else
				{
					slang_type_specifier_type type = slang_type_specifier_type_from_string (
						op->identifier);
					if (type == slang_spec_void)
						return 0;
					ti->spec.type = type;
				}
			}
		}
		break;
	case slang_oper_field:
		{
			slang_assembly_typeinfo _ti;
			slang_assembly_typeinfo_construct (&_ti);
			if (!_slang_typeof_operation (op->children, space, &_ti))
			{
				slang_assembly_typeinfo_destruct (&_ti);
				return 0;
			}
			if (_ti.spec.type == slang_spec_struct)
			{
				slang_variable *field = _slang_locate_variable (_ti.spec._struct->fields,
					op->identifier, 0);
				if (field == NULL)
				{
					slang_assembly_typeinfo_destruct (&_ti);
					return 0;
				}
				if (!slang_type_specifier_copy (&ti->spec, &field->type.specifier))
				{
					slang_assembly_typeinfo_destruct (&_ti);
					return 0;
				}
			}
			else
			{
				unsigned int rows;
				switch (_ti.spec.type)
				{
				case slang_spec_vec2:
				case slang_spec_ivec2:
				case slang_spec_bvec2:
					rows = 2;
					break;
				case slang_spec_vec3:
				case slang_spec_ivec3:
				case slang_spec_bvec3:
					rows = 3;
					break;
				case slang_spec_vec4:
				case slang_spec_ivec4:
				case slang_spec_bvec4:
					rows = 4;
					break;
				default:
					slang_assembly_typeinfo_destruct (&_ti);
					return 0;
				}
				if (!_slang_is_swizzle (op->identifier, rows, &ti->swz))
					return 0;
				ti->is_swizzled = 1;
				ti->can_be_referenced = _ti.can_be_referenced && _slang_is_swizzle_mask (&ti->swz,
					rows);
				if (_ti.is_swizzled)
				{
					slang_swizzle swz;
					_slang_multiply_swizzles (&swz, &_ti.swz, &ti->swz);
					ti->swz = swz;
				}
				switch (_ti.spec.type)
				{
				case slang_spec_vec2:
				case slang_spec_vec3:
				case slang_spec_vec4:
					switch (ti->swz.num_components)
					{
					case 1:
						ti->spec.type = slang_spec_float;
						break;
					case 2:
						ti->spec.type = slang_spec_vec2;
						break;
					case 3:
						ti->spec.type = slang_spec_vec3;
						break;
					case 4:
						ti->spec.type = slang_spec_vec4;
						break;
					}
					break;
				case slang_spec_ivec2:
				case slang_spec_ivec3:
				case slang_spec_ivec4:
					switch (ti->swz.num_components)
					{
					case 1:
						ti->spec.type = slang_spec_int;
						break;
					case 2:
						ti->spec.type = slang_spec_ivec2;
						break;
					case 3:
						ti->spec.type = slang_spec_ivec3;
						break;
					case 4:
						ti->spec.type = slang_spec_ivec4;
						break;
					}
					break;
				case slang_spec_bvec2:
				case slang_spec_bvec3:
				case slang_spec_bvec4:
					switch (ti->swz.num_components)
					{
					case 1:
						ti->spec.type = slang_spec_bool;
						break;
					case 2:
						ti->spec.type = slang_spec_bvec2;
						break;
					case 3:
						ti->spec.type = slang_spec_bvec3;
						break;
					case 4:
						ti->spec.type = slang_spec_bvec4;
						break;
					}
					break;
				default:
				   break;
				}
			}
			slang_assembly_typeinfo_destruct (&_ti);
			return 1;
		}
		break;
	case slang_oper_postincrement:
	case slang_oper_postdecrement:
		if (!_slang_typeof_operation (op->children, space, ti))
			return 0;
		ti->can_be_referenced = 0;
		ti->is_swizzled = 0;
		break;
	default:
		return 0;
	}
	return 1;
}

/* _slang_typeof_function() */

int _slang_typeof_function (const char *name, slang_operation *params, unsigned int num_params,
	slang_assembly_name_space *space, slang_type_specifier *spec, int *exists)
{
	slang_function *fun = _slang_locate_function (name, params, num_params, space);
	*exists = fun != NULL;
	if (fun == NULL)
		return 1;
	return slang_type_specifier_copy (spec, &fun->header.type.specifier);
}