aboutsummaryrefslogtreecommitdiff
path: root/nx-X11/extras/Mesa/src/mesa/shader/slang/slang_assemble_assignment.c
blob: 488c7a91ff5b04f4430aeb334881c0ba968dcff1 (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
/*
 * 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_assignment.c
 * slang assignment expressions assembler
 * \author Michal Krol
 */

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

/*
	_slang_assemble_assignment()

	copies values on the stack (<component 0> to <component N-1>) to a memory
	location pointed by <addr of variable>;

	in:
		+------------------+
		| addr of variable |
		+------------------+
		| component N-1    |
		| ...              |
		| component 0      |
		+------------------+

	out:
		+------------------+
		| addr of variable |
		+------------------+
*/
/* TODO: add support for swizzle mask */
static int assign_aggregate (slang_assembly_file *file, const slang_storage_aggregate *agg,
	unsigned int *index, unsigned int size, slang_assembly_local_info *info)
{
	unsigned int i;

	for (i = 0; i < agg->count; i++)
	{
		const slang_storage_array *arr = agg->arrays + i;
		unsigned int j;

		for (j = 0; j < arr->length; j++)
		{
			if (arr->type == slang_stor_aggregate)
			{
				if (!assign_aggregate (file, arr->aggregate, index, size, info))
					return 0;
			}
			else
			{
				slang_assembly_type ty;

				switch (arr->type)
				{
				case slang_stor_bool:
					ty = slang_asm_bool_copy;
					break;
				case slang_stor_int:
					ty = slang_asm_int_copy;
					break;
				case slang_stor_float:
					ty = slang_asm_float_copy;
					break;
				default:
					break;
				}
				if (!slang_assembly_file_push_label2 (file, ty, size - *index, *index))
					return 0;
				*index += 4;
			}
		}
	}
	return 1;
}

int _slang_assemble_assignment (slang_assembly_file *file, slang_operation *op,
	slang_assembly_name_space *space, slang_assembly_local_info *info)
{
	slang_assembly_typeinfo ti;
	int result;
	slang_storage_aggregate agg;
	unsigned int index, size;

	slang_assembly_typeinfo_construct (&ti);
	if (!_slang_typeof_operation (op, space, &ti))
	{
		slang_assembly_typeinfo_destruct (&ti);
		return 0;
	}

	slang_storage_aggregate_construct (&agg);
	if (!_slang_aggregate_variable (&agg, &ti.spec, NULL, space->funcs, space->structs))
	{
		slang_storage_aggregate_destruct (&agg);
		slang_assembly_typeinfo_destruct (&ti);
		return 0;
	}

	index = 0;
	size = _slang_sizeof_aggregate (&agg);
	result = assign_aggregate (file, &agg, &index, size, info);

	slang_storage_aggregate_destruct (&agg);
	slang_assembly_typeinfo_destruct (&ti);
	return result;
}

/*
	_slang_assemble_assign()

	performs unary (pre ++ and --) or binary (=, +=, -=, *=, /=) assignment on the operation's
	children
*/

int dereference (slang_assembly_file *file, slang_operation *op,
	slang_assembly_name_space *space, slang_assembly_local_info *info);

int call_function_name (slang_assembly_file *file, const char *name, slang_operation *params,
	unsigned int param_count, int assignment, slang_assembly_name_space *space,
	slang_assembly_local_info *info);

int _slang_assemble_assign (slang_assembly_file *file, slang_operation *op, const char *oper,
	int ref, slang_assembly_name_space *space, slang_assembly_local_info *info)
{
	slang_assembly_stack_info stk;
	slang_assembly_flow_control flow;

	if (!ref)
	{
		if (!slang_assembly_file_push_label2 (file, slang_asm_local_addr, info->addr_tmp, 4))
			return 0;
	}

	if (slang_string_compare ("=", oper) == 0)
	{
		if (!_slang_assemble_operation (file, op->children, 1, &flow, space, info, &stk))
			return 0;
		if (!_slang_assemble_operation (file, op->children + 1, 0, &flow, space, info, &stk))
			return 0;
		if (!_slang_assemble_assignment (file, op->children, space, info))
			return 0;
	}
	else
	{
		if (!call_function_name (file, oper, op->children, op->num_children, 1, space, info))
			return 0;
	}

	if (!ref)
	{
		if (!slang_assembly_file_push (file, slang_asm_addr_copy))
			return 0;
		if (!slang_assembly_file_push_label (file, slang_asm_local_free, 4))
			return 0;
		if (!dereference (file, op->children, space, info))
			return 0;
	}

	return 1;
}