r300_tgsi_to_rc.c revision d1b4351e603522be11061522cb6b685da9ef1fee
1188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle/*
2188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle * Copyright 2009 Nicolai Hähnle <nhaehnle@gmail.com>
3188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle *
4188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle * Permission is hereby granted, free of charge, to any person obtaining a
5188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle * copy of this software and associated documentation files (the "Software"),
6188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle * to deal in the Software without restriction, including without limitation
7188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle * on the rights to use, copy, modify, merge, publish, distribute, sub
8188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle * license, and/or sell copies of the Software, and to permit persons to whom
9188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle * the Software is furnished to do so, subject to the following conditions:
10188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle *
11188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle * The above copyright notice and this permission notice (including the next
12188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle * paragraph) shall be included in all copies or substantial portions of the
13188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle * Software.
14188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle *
15188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle * THE COPYRIGHT HOLDER(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle * USE OR OTHER DEALINGS IN THE SOFTWARE. */
22188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle
23188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle#include "r300_tgsi_to_rc.h"
24188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle
25188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle#include "radeon_compiler.h"
26188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle#include "radeon_program.h"
27188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle
28188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle#include "tgsi/tgsi_parse.h"
29188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle#include "tgsi/tgsi_scan.h"
30188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle#include "tgsi/tgsi_util.h"
31188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle
32188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle
33188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnlestatic unsigned translate_opcode(unsigned opcode)
34188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle{
35188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle    switch(opcode) {
36d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle        case TGSI_OPCODE_ARL: return RC_OPCODE_ARL;
37d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle        case TGSI_OPCODE_MOV: return RC_OPCODE_MOV;
38d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle        case TGSI_OPCODE_LIT: return RC_OPCODE_LIT;
39d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle        case TGSI_OPCODE_RCP: return RC_OPCODE_RCP;
40d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle        case TGSI_OPCODE_RSQ: return RC_OPCODE_RSQ;
41d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle        case TGSI_OPCODE_EXP: return RC_OPCODE_EXP;
42d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle        case TGSI_OPCODE_LOG: return RC_OPCODE_LOG;
43d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle        case TGSI_OPCODE_MUL: return RC_OPCODE_MUL;
44d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle        case TGSI_OPCODE_ADD: return RC_OPCODE_ADD;
45d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle        case TGSI_OPCODE_DP3: return RC_OPCODE_DP3;
46d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle        case TGSI_OPCODE_DP4: return RC_OPCODE_DP4;
47d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle        case TGSI_OPCODE_DST: return RC_OPCODE_DST;
48d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle        case TGSI_OPCODE_MIN: return RC_OPCODE_MIN;
49d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle        case TGSI_OPCODE_MAX: return RC_OPCODE_MAX;
50d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle        case TGSI_OPCODE_SLT: return RC_OPCODE_SLT;
51d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle        case TGSI_OPCODE_SGE: return RC_OPCODE_SGE;
52d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle        case TGSI_OPCODE_MAD: return RC_OPCODE_MAD;
53d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle        case TGSI_OPCODE_SUB: return RC_OPCODE_SUB;
54d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle        case TGSI_OPCODE_LRP: return RC_OPCODE_LRP;
55d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_CND: return RC_OPCODE_CND; */
56d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_CND0: return RC_OPCODE_CND0; */
57d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_DP2A: return RC_OPCODE_DP2A; */
58188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle                                        /* gap */
59d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle        case TGSI_OPCODE_FRC: return RC_OPCODE_FRC;
60d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_CLAMP: return RC_OPCODE_CLAMP; */
61d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle        case TGSI_OPCODE_FLR: return RC_OPCODE_FLR;
62d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_ROUND: return RC_OPCODE_ROUND; */
63d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle        case TGSI_OPCODE_EX2: return RC_OPCODE_EX2;
64d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle        case TGSI_OPCODE_LG2: return RC_OPCODE_LG2;
65d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle        case TGSI_OPCODE_POW: return RC_OPCODE_POW;
66d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle        case TGSI_OPCODE_XPD: return RC_OPCODE_XPD;
67188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle                                        /* gap */
68d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle        case TGSI_OPCODE_ABS: return RC_OPCODE_ABS;
69d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_RCC: return RC_OPCODE_RCC; */
70d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle        case TGSI_OPCODE_DPH: return RC_OPCODE_DPH;
71d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle        case TGSI_OPCODE_COS: return RC_OPCODE_COS;
72d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle        case TGSI_OPCODE_DDX: return RC_OPCODE_DDX;
73d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle        case TGSI_OPCODE_DDY: return RC_OPCODE_DDY;
74d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_KILP: return RC_OPCODE_KILP; */
75d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_PK2H: return RC_OPCODE_PK2H; */
76d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_PK2US: return RC_OPCODE_PK2US; */
77d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_PK4B: return RC_OPCODE_PK4B; */
78d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_PK4UB: return RC_OPCODE_PK4UB; */
79d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_RFL: return RC_OPCODE_RFL; */
80d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle        case TGSI_OPCODE_SEQ: return RC_OPCODE_SEQ;
81d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle        case TGSI_OPCODE_SFL: return RC_OPCODE_SFL;
82d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle        case TGSI_OPCODE_SGT: return RC_OPCODE_SGT;
83d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle        case TGSI_OPCODE_SIN: return RC_OPCODE_SIN;
84d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle        case TGSI_OPCODE_SLE: return RC_OPCODE_SLE;
85d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle        case TGSI_OPCODE_SNE: return RC_OPCODE_SNE;
86d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_STR: return RC_OPCODE_STR; */
87d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle        case TGSI_OPCODE_TEX: return RC_OPCODE_TEX;
88d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle        case TGSI_OPCODE_TXD: return RC_OPCODE_TXD;
89d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle        case TGSI_OPCODE_TXP: return RC_OPCODE_TXP;
90d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_UP2H: return RC_OPCODE_UP2H; */
91d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_UP2US: return RC_OPCODE_UP2US; */
92d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_UP4B: return RC_OPCODE_UP4B; */
93d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_UP4UB: return RC_OPCODE_UP4UB; */
94d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_X2D: return RC_OPCODE_X2D; */
95d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_ARA: return RC_OPCODE_ARA; */
96d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_ARR: return RC_OPCODE_ARR; */
97d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_BRA: return RC_OPCODE_BRA; */
98d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_CAL: return RC_OPCODE_CAL; */
99d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_RET: return RC_OPCODE_RET; */
100d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_SSG: return RC_OPCODE_SSG; */
101d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle        case TGSI_OPCODE_CMP: return RC_OPCODE_CMP;
102d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle        case TGSI_OPCODE_SCS: return RC_OPCODE_SCS;
103d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle        case TGSI_OPCODE_TXB: return RC_OPCODE_TXB;
104d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_NRM: return RC_OPCODE_NRM; */
105d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_DIV: return RC_OPCODE_DIV; */
106d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_DP2: return RC_OPCODE_DP2; */
107d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle        case TGSI_OPCODE_TXL: return RC_OPCODE_TXL;
108d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_BRK: return RC_OPCODE_BRK; */
109d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_IF: return RC_OPCODE_IF; */
110d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_LOOP: return RC_OPCODE_LOOP; */
111d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_REP: return RC_OPCODE_REP; */
112d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_ELSE: return RC_OPCODE_ELSE; */
113d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_ENDIF: return RC_OPCODE_ENDIF; */
114d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_ENDLOOP: return RC_OPCODE_ENDLOOP; */
115d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_ENDREP: return RC_OPCODE_ENDREP; */
116d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_PUSHA: return RC_OPCODE_PUSHA; */
117d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_POPA: return RC_OPCODE_POPA; */
118d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_CEIL: return RC_OPCODE_CEIL; */
119d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_I2F: return RC_OPCODE_I2F; */
120d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_NOT: return RC_OPCODE_NOT; */
121d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_TRUNC: return RC_OPCODE_TRUNC; */
122d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_SHL: return RC_OPCODE_SHL; */
123d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_SHR: return RC_OPCODE_SHR; */
124d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_AND: return RC_OPCODE_AND; */
125d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_OR: return RC_OPCODE_OR; */
126d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_MOD: return RC_OPCODE_MOD; */
127d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_XOR: return RC_OPCODE_XOR; */
128d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_SAD: return RC_OPCODE_SAD; */
129d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_TXF: return RC_OPCODE_TXF; */
130d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_TXQ: return RC_OPCODE_TXQ; */
131d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_CONT: return RC_OPCODE_CONT; */
132d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_EMIT: return RC_OPCODE_EMIT; */
133d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_ENDPRIM: return RC_OPCODE_ENDPRIM; */
134d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_BGNLOOP2: return RC_OPCODE_BGNLOOP2; */
135d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_BGNSUB: return RC_OPCODE_BGNSUB; */
136d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_ENDLOOP2: return RC_OPCODE_ENDLOOP2; */
137d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_ENDSUB: return RC_OPCODE_ENDSUB; */
138d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_NOISE1: return RC_OPCODE_NOISE1; */
139d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_NOISE2: return RC_OPCODE_NOISE2; */
140d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_NOISE3: return RC_OPCODE_NOISE3; */
141d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_NOISE4: return RC_OPCODE_NOISE4; */
142d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle        case TGSI_OPCODE_NOP: return RC_OPCODE_NOP;
143188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle                                        /* gap */
144d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_NRM4: return RC_OPCODE_NRM4; */
145d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_CALLNZ: return RC_OPCODE_CALLNZ; */
146d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_IFC: return RC_OPCODE_IFC; */
147d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle     /* case TGSI_OPCODE_BREAKC: return RC_OPCODE_BREAKC; */
148d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle        case TGSI_OPCODE_KIL: return RC_OPCODE_KIL;
149d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle        case TGSI_OPCODE_SWZ: return RC_OPCODE_SWZ;
150188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle    }
151188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle
152188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle    fprintf(stderr, "Unknown opcode: %i\n", opcode);
153d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle    return RC_OPCODE_ILLEGAL_OPCODE;
154188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle}
155188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle
156188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnlestatic unsigned translate_saturate(unsigned saturate)
157188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle{
158188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle    switch(saturate) {
159d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle        default:
160d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle            fprintf(stderr, "Unknown saturate mode: %i\n", saturate);
161d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle            /* fall-through */
162d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle        case TGSI_SAT_NONE: return RC_SATURATE_NONE;
163d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle        case TGSI_SAT_ZERO_ONE: return RC_SATURATE_ZERO_ONE;
164d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle        case TGSI_SAT_MINUS_PLUS_ONE: return RC_SATURATE_MINUS_PLUS_ONE;
165188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle    }
166188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle}
167188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle
168188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnlestatic unsigned translate_register_file(unsigned file)
169188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle{
170188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle    switch(file) {
171d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle        case TGSI_FILE_CONSTANT: return RC_FILE_CONSTANT;
172d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle        case TGSI_FILE_IMMEDIATE: return RC_FILE_CONSTANT;
173d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle        case TGSI_FILE_INPUT: return RC_FILE_INPUT;
174d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle        case TGSI_FILE_OUTPUT: return RC_FILE_OUTPUT;
175d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle        default:
176d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle            fprintf(stderr, "Unhandled register file: %i\n", file);
177d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle            /* fall-through */
178d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle        case TGSI_FILE_TEMPORARY: return RC_FILE_TEMPORARY;
179d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle        case TGSI_FILE_ADDRESS: return RC_FILE_ADDRESS;
180188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle    }
181188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle}
182188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle
183188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnlestatic int translate_register_index(
184188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle    struct tgsi_to_rc * ttr,
185188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle    unsigned file,
186188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle    int index)
187188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle{
188188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle    if (file == TGSI_FILE_IMMEDIATE)
189188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle        return ttr->immediate_offset + index;
190188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle
191188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle    return index;
192188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle}
193188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle
194188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnlestatic void transform_dstreg(
195188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle    struct tgsi_to_rc * ttr,
196d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle    struct rc_dst_register * dst,
197188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle    struct tgsi_full_dst_register * src)
198188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle{
199188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle    dst->File = translate_register_file(src->DstRegister.File);
200188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle    dst->Index = translate_register_index(ttr, src->DstRegister.File, src->DstRegister.Index);
201188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle    dst->WriteMask = src->DstRegister.WriteMask;
202188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle    dst->RelAddr = src->DstRegister.Indirect;
203188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle}
204188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle
205188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnlestatic void transform_srcreg(
206188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle    struct tgsi_to_rc * ttr,
207d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle    struct rc_src_register * dst,
208188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle    struct tgsi_full_src_register * src)
209188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle{
210188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle    dst->File = translate_register_file(src->SrcRegister.File);
211188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle    dst->Index = translate_register_index(ttr, src->SrcRegister.File, src->SrcRegister.Index);
212188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle    dst->RelAddr = src->SrcRegister.Indirect;
213188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle    dst->Swizzle = tgsi_util_get_full_src_register_extswizzle(src, 0);
214188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle    dst->Swizzle |= tgsi_util_get_full_src_register_extswizzle(src, 1) << 3;
215188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle    dst->Swizzle |= tgsi_util_get_full_src_register_extswizzle(src, 2) << 6;
216188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle    dst->Swizzle |= tgsi_util_get_full_src_register_extswizzle(src, 3) << 9;
217188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle    dst->Abs = src->SrcRegisterExtMod.Absolute;
218188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle    dst->Negate =
219188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle        src->SrcRegisterExtSwz.NegateX |
220188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle        (src->SrcRegisterExtSwz.NegateY << 1) |
221188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle        (src->SrcRegisterExtSwz.NegateZ << 2) |
222188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle        (src->SrcRegisterExtSwz.NegateW << 3);
223d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle    dst->Negate ^= src->SrcRegister.Negate ? RC_MASK_XYZW : 0;
224188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle}
225188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle
226d0c398a8e2985b855f923aec3470cef8734a622aNicolai Hähnlestatic void transform_texture(struct rc_instruction * dst, struct tgsi_instruction_ext_texture src)
227d0c398a8e2985b855f923aec3470cef8734a622aNicolai Hähnle{
228d0c398a8e2985b855f923aec3470cef8734a622aNicolai Hähnle    switch(src.Texture) {
229d0c398a8e2985b855f923aec3470cef8734a622aNicolai Hähnle        case TGSI_TEXTURE_1D:
230d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle            dst->I.TexSrcTarget = RC_TEXTURE_1D;
231d0c398a8e2985b855f923aec3470cef8734a622aNicolai Hähnle            break;
232d0c398a8e2985b855f923aec3470cef8734a622aNicolai Hähnle        case TGSI_TEXTURE_2D:
233d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle            dst->I.TexSrcTarget = RC_TEXTURE_2D;
234d0c398a8e2985b855f923aec3470cef8734a622aNicolai Hähnle            break;
235d0c398a8e2985b855f923aec3470cef8734a622aNicolai Hähnle        case TGSI_TEXTURE_3D:
236d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle            dst->I.TexSrcTarget = RC_TEXTURE_3D;
237d0c398a8e2985b855f923aec3470cef8734a622aNicolai Hähnle            break;
238d0c398a8e2985b855f923aec3470cef8734a622aNicolai Hähnle        case TGSI_TEXTURE_CUBE:
239d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle            dst->I.TexSrcTarget = RC_TEXTURE_CUBE;
240d0c398a8e2985b855f923aec3470cef8734a622aNicolai Hähnle            break;
241d0c398a8e2985b855f923aec3470cef8734a622aNicolai Hähnle        case TGSI_TEXTURE_RECT:
242d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle            dst->I.TexSrcTarget = RC_TEXTURE_RECT;
243d0c398a8e2985b855f923aec3470cef8734a622aNicolai Hähnle            break;
244d0c398a8e2985b855f923aec3470cef8734a622aNicolai Hähnle        case TGSI_TEXTURE_SHADOW1D:
245d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle            dst->I.TexSrcTarget = RC_TEXTURE_1D;
246d0c398a8e2985b855f923aec3470cef8734a622aNicolai Hähnle            dst->I.TexShadow = 1;
247d0c398a8e2985b855f923aec3470cef8734a622aNicolai Hähnle            break;
248d0c398a8e2985b855f923aec3470cef8734a622aNicolai Hähnle        case TGSI_TEXTURE_SHADOW2D:
249d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle            dst->I.TexSrcTarget = RC_TEXTURE_2D;
250d0c398a8e2985b855f923aec3470cef8734a622aNicolai Hähnle            dst->I.TexShadow = 1;
251d0c398a8e2985b855f923aec3470cef8734a622aNicolai Hähnle            break;
252d0c398a8e2985b855f923aec3470cef8734a622aNicolai Hähnle        case TGSI_TEXTURE_SHADOWRECT:
253d1b4351e603522be11061522cb6b685da9ef1feeNicolai Hähnle            dst->I.TexSrcTarget = RC_TEXTURE_RECT;
254d0c398a8e2985b855f923aec3470cef8734a622aNicolai Hähnle            dst->I.TexShadow = 1;
255d0c398a8e2985b855f923aec3470cef8734a622aNicolai Hähnle            break;
256d0c398a8e2985b855f923aec3470cef8734a622aNicolai Hähnle    }
257d0c398a8e2985b855f923aec3470cef8734a622aNicolai Hähnle}
258d0c398a8e2985b855f923aec3470cef8734a622aNicolai Hähnle
259188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnlestatic void transform_instruction(struct tgsi_to_rc * ttr, struct tgsi_full_instruction * src)
260188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle{
261188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle    if (src->Instruction.Opcode == TGSI_OPCODE_END)
262188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle        return;
263188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle
264188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle    struct rc_instruction * dst = rc_insert_new_instruction(ttr->compiler, ttr->compiler->Program.Instructions.Prev);
265188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle    int i;
266188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle
267188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle    dst->I.Opcode = translate_opcode(src->Instruction.Opcode);
268188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle    dst->I.SaturateMode = translate_saturate(src->Instruction.Saturate);
269188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle
270188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle    if (src->Instruction.NumDstRegs)
271188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle        transform_dstreg(ttr, &dst->I.DstReg, &src->FullDstRegisters[0]);
272188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle
273d0c398a8e2985b855f923aec3470cef8734a622aNicolai Hähnle    for(i = 0; i < src->Instruction.NumSrcRegs; ++i) {
274d0c398a8e2985b855f923aec3470cef8734a622aNicolai Hähnle        if (src->FullSrcRegisters[i].SrcRegister.File == TGSI_FILE_SAMPLER)
275d0c398a8e2985b855f923aec3470cef8734a622aNicolai Hähnle            dst->I.TexSrcUnit = src->FullSrcRegisters[i].SrcRegister.Index;
276d0c398a8e2985b855f923aec3470cef8734a622aNicolai Hähnle        else
277d0c398a8e2985b855f923aec3470cef8734a622aNicolai Hähnle            transform_srcreg(ttr, &dst->I.SrcReg[i], &src->FullSrcRegisters[i]);
278d0c398a8e2985b855f923aec3470cef8734a622aNicolai Hähnle    }
279188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle
280d0c398a8e2985b855f923aec3470cef8734a622aNicolai Hähnle    /* Texturing. */
281d0c398a8e2985b855f923aec3470cef8734a622aNicolai Hähnle    transform_texture(dst, src->InstructionExtTexture);
282188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle}
283188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle
284188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnlestatic void handle_immediate(struct tgsi_to_rc * ttr, struct tgsi_full_immediate * imm)
285188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle{
286188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle    struct rc_constant constant;
287188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle    int i;
288188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle
289188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle    constant.Type = RC_CONSTANT_IMMEDIATE;
290188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle    constant.Size = 4;
291188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle    for(i = 0; i < 4; ++i)
292188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle        constant.u.Immediate[i] = imm->u[i].Float;
293188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle    rc_constants_add(&ttr->compiler->Program.Constants, &constant);
294188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle}
295188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle
296188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnlevoid r300_tgsi_to_rc(struct tgsi_to_rc * ttr, const struct tgsi_token * tokens)
297188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle{
298188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle    struct tgsi_parse_context parser;
299188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle    int i;
300188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle
301188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle    /* Allocate constants placeholders.
302188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle     *
303188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle     * Note: What if declared constants are not contiguous? */
304188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle    for(i = 0; i <= ttr->info->file_max[TGSI_FILE_CONSTANT]; ++i) {
305188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle        struct rc_constant constant;
306188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle        memset(&constant, 0, sizeof(constant));
307188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle        constant.Type = RC_CONSTANT_EXTERNAL;
308188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle        constant.Size = 4;
309188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle        constant.u.External = i;
310188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle        rc_constants_add(&ttr->compiler->Program.Constants, &constant);
311188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle    }
312188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle
313188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle    ttr->immediate_offset = ttr->compiler->Program.Constants.Count;
314188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle
315188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle    tgsi_parse_init(&parser, tokens);
316188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle
317188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle    while (!tgsi_parse_end_of_tokens(&parser)) {
318188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle        tgsi_parse_token(&parser);
319188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle
320188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle        switch (parser.FullToken.Token.Type) {
321188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle            case TGSI_TOKEN_TYPE_DECLARATION:
322188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle                break;
323188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle            case TGSI_TOKEN_TYPE_IMMEDIATE:
324188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle                handle_immediate(ttr, &parser.FullToken.FullImmediate);
325188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle                break;
326188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle            case TGSI_TOKEN_TYPE_INSTRUCTION:
327188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle                transform_instruction(ttr, &parser.FullToken.FullInstruction);
328188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle                break;
329188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle        }
330188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle    }
331188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle
332188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle    tgsi_parse_free(&parser);
333188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle
334188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle    rc_calculate_inputs_outputs(ttr->compiler);
335188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle}
336188f8c679254f193cdcfcd4ef338f3c8c5e1146dNicolai Hähnle
337