1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Mesa 3-D graphics library
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
5bf215546Sopenharmony_ci * Copyright (C) 1999-2009  VMware, Inc.  All Rights Reserved.
6bf215546Sopenharmony_ci *
7bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
8bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
9bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
10bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
12bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
13bf215546Sopenharmony_ci *
14bf215546Sopenharmony_ci * The above copyright notice and this permission notice shall be included
15bf215546Sopenharmony_ci * in all copies or substantial portions of the Software.
16bf215546Sopenharmony_ci *
17bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21bf215546Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22bf215546Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23bf215546Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE.
24bf215546Sopenharmony_ci */
25bf215546Sopenharmony_ci
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#include <stdio.h>
28bf215546Sopenharmony_ci#include <assert.h>
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ci#include "main/glheader.h"
31bf215546Sopenharmony_ci#include "prog_instruction.h"
32bf215546Sopenharmony_ci#include "prog_parameter.h"
33bf215546Sopenharmony_ci
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_ci/**
36bf215546Sopenharmony_ci * Initialize program instruction fields to defaults.
37bf215546Sopenharmony_ci * \param inst  first instruction to initialize
38bf215546Sopenharmony_ci * \param count  number of instructions to initialize
39bf215546Sopenharmony_ci */
40bf215546Sopenharmony_civoid
41bf215546Sopenharmony_ci_mesa_init_instructions(struct prog_instruction *inst, GLuint count)
42bf215546Sopenharmony_ci{
43bf215546Sopenharmony_ci   GLuint i;
44bf215546Sopenharmony_ci
45bf215546Sopenharmony_ci   memset(inst, 0, count * sizeof(struct prog_instruction));
46bf215546Sopenharmony_ci
47bf215546Sopenharmony_ci   for (i = 0; i < count; i++) {
48bf215546Sopenharmony_ci      inst[i].SrcReg[0].File = PROGRAM_UNDEFINED;
49bf215546Sopenharmony_ci      inst[i].SrcReg[0].Swizzle = SWIZZLE_NOOP;
50bf215546Sopenharmony_ci      inst[i].SrcReg[1].File = PROGRAM_UNDEFINED;
51bf215546Sopenharmony_ci      inst[i].SrcReg[1].Swizzle = SWIZZLE_NOOP;
52bf215546Sopenharmony_ci      inst[i].SrcReg[2].File = PROGRAM_UNDEFINED;
53bf215546Sopenharmony_ci      inst[i].SrcReg[2].Swizzle = SWIZZLE_NOOP;
54bf215546Sopenharmony_ci
55bf215546Sopenharmony_ci      inst[i].DstReg.File = PROGRAM_UNDEFINED;
56bf215546Sopenharmony_ci      inst[i].DstReg.WriteMask = WRITEMASK_XYZW;
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_ci      inst[i].Saturate = GL_FALSE;
59bf215546Sopenharmony_ci   }
60bf215546Sopenharmony_ci}
61bf215546Sopenharmony_ci
62bf215546Sopenharmony_ci
63bf215546Sopenharmony_ci/**
64bf215546Sopenharmony_ci * Copy an array of program instructions.
65bf215546Sopenharmony_ci * \param dest  pointer to destination.
66bf215546Sopenharmony_ci * \param src  pointer to source.
67bf215546Sopenharmony_ci * \param n  number of instructions to copy.
68bf215546Sopenharmony_ci * \return pointer to destination.
69bf215546Sopenharmony_ci */
70bf215546Sopenharmony_cistruct prog_instruction *
71bf215546Sopenharmony_ci_mesa_copy_instructions(struct prog_instruction *dest,
72bf215546Sopenharmony_ci                        const struct prog_instruction *src, GLuint n)
73bf215546Sopenharmony_ci{
74bf215546Sopenharmony_ci   memcpy(dest, src, n * sizeof(struct prog_instruction));
75bf215546Sopenharmony_ci   return dest;
76bf215546Sopenharmony_ci}
77bf215546Sopenharmony_ci
78bf215546Sopenharmony_ci
79bf215546Sopenharmony_ci/**
80bf215546Sopenharmony_ci * Basic info about each instruction
81bf215546Sopenharmony_ci */
82bf215546Sopenharmony_cistruct instruction_info
83bf215546Sopenharmony_ci{
84bf215546Sopenharmony_ci   enum prog_opcode Opcode;
85bf215546Sopenharmony_ci   const char *Name;
86bf215546Sopenharmony_ci   GLuint NumSrcRegs;
87bf215546Sopenharmony_ci   GLuint NumDstRegs;
88bf215546Sopenharmony_ci};
89bf215546Sopenharmony_ci
90bf215546Sopenharmony_ci/**
91bf215546Sopenharmony_ci * Instruction info
92bf215546Sopenharmony_ci * \note Opcode should equal array index!
93bf215546Sopenharmony_ci */
94bf215546Sopenharmony_cistatic const struct instruction_info InstInfo[MAX_OPCODE] = {
95bf215546Sopenharmony_ci   { OPCODE_NOP,    "NOP",     0, 0 },
96bf215546Sopenharmony_ci   { OPCODE_ABS,    "ABS",     1, 1 },
97bf215546Sopenharmony_ci   { OPCODE_ADD,    "ADD",     2, 1 },
98bf215546Sopenharmony_ci   { OPCODE_ARL,    "ARL",     1, 1 },
99bf215546Sopenharmony_ci   { OPCODE_BGNLOOP,"BGNLOOP", 0, 0 },
100bf215546Sopenharmony_ci   { OPCODE_BGNSUB, "BGNSUB",  0, 0 },
101bf215546Sopenharmony_ci   { OPCODE_BRK,    "BRK",     0, 0 },
102bf215546Sopenharmony_ci   { OPCODE_CAL,    "CAL",     0, 0 },
103bf215546Sopenharmony_ci   { OPCODE_CMP,    "CMP",     3, 1 },
104bf215546Sopenharmony_ci   { OPCODE_CONT,   "CONT",    0, 0 },
105bf215546Sopenharmony_ci   { OPCODE_COS,    "COS",     1, 1 },
106bf215546Sopenharmony_ci   { OPCODE_DDX,    "DDX",     1, 1 },
107bf215546Sopenharmony_ci   { OPCODE_DDY,    "DDY",     1, 1 },
108bf215546Sopenharmony_ci   { OPCODE_DP2,    "DP2",     2, 1 },
109bf215546Sopenharmony_ci   { OPCODE_DP3,    "DP3",     2, 1 },
110bf215546Sopenharmony_ci   { OPCODE_DP4,    "DP4",     2, 1 },
111bf215546Sopenharmony_ci   { OPCODE_DPH,    "DPH",     2, 1 },
112bf215546Sopenharmony_ci   { OPCODE_DST,    "DST",     2, 1 },
113bf215546Sopenharmony_ci   { OPCODE_ELSE,   "ELSE",    0, 0 },
114bf215546Sopenharmony_ci   { OPCODE_END,    "END",     0, 0 },
115bf215546Sopenharmony_ci   { OPCODE_ENDIF,  "ENDIF",   0, 0 },
116bf215546Sopenharmony_ci   { OPCODE_ENDLOOP,"ENDLOOP", 0, 0 },
117bf215546Sopenharmony_ci   { OPCODE_ENDSUB, "ENDSUB",  0, 0 },
118bf215546Sopenharmony_ci   { OPCODE_EX2,    "EX2",     1, 1 },
119bf215546Sopenharmony_ci   { OPCODE_EXP,    "EXP",     1, 1 },
120bf215546Sopenharmony_ci   { OPCODE_FLR,    "FLR",     1, 1 },
121bf215546Sopenharmony_ci   { OPCODE_FRC,    "FRC",     1, 1 },
122bf215546Sopenharmony_ci   { OPCODE_IF,     "IF",      1, 0 },
123bf215546Sopenharmony_ci   { OPCODE_KIL,    "KIL",     1, 0 },
124bf215546Sopenharmony_ci   { OPCODE_LG2,    "LG2",     1, 1 },
125bf215546Sopenharmony_ci   { OPCODE_LIT,    "LIT",     1, 1 },
126bf215546Sopenharmony_ci   { OPCODE_LOG,    "LOG",     1, 1 },
127bf215546Sopenharmony_ci   { OPCODE_LRP,    "LRP",     3, 1 },
128bf215546Sopenharmony_ci   { OPCODE_MAD,    "MAD",     3, 1 },
129bf215546Sopenharmony_ci   { OPCODE_MAX,    "MAX",     2, 1 },
130bf215546Sopenharmony_ci   { OPCODE_MIN,    "MIN",     2, 1 },
131bf215546Sopenharmony_ci   { OPCODE_MOV,    "MOV",     1, 1 },
132bf215546Sopenharmony_ci   { OPCODE_MUL,    "MUL",     2, 1 },
133bf215546Sopenharmony_ci   { OPCODE_NOISE1, "NOISE1",  1, 1 },
134bf215546Sopenharmony_ci   { OPCODE_NOISE2, "NOISE2",  1, 1 },
135bf215546Sopenharmony_ci   { OPCODE_NOISE3, "NOISE3",  1, 1 },
136bf215546Sopenharmony_ci   { OPCODE_NOISE4, "NOISE4",  1, 1 },
137bf215546Sopenharmony_ci   { OPCODE_POW,    "POW",     2, 1 },
138bf215546Sopenharmony_ci   { OPCODE_RCP,    "RCP",     1, 1 },
139bf215546Sopenharmony_ci   { OPCODE_RET,    "RET",     0, 0 },
140bf215546Sopenharmony_ci   { OPCODE_RSQ,    "RSQ",     1, 1 },
141bf215546Sopenharmony_ci   { OPCODE_SCS,    "SCS",     1, 1 },
142bf215546Sopenharmony_ci   { OPCODE_SGE,    "SGE",     2, 1 },
143bf215546Sopenharmony_ci   { OPCODE_SIN,    "SIN",     1, 1 },
144bf215546Sopenharmony_ci   { OPCODE_SLT,    "SLT",     2, 1 },
145bf215546Sopenharmony_ci   { OPCODE_SSG,    "SSG",     1, 1 },
146bf215546Sopenharmony_ci   { OPCODE_SUB,    "SUB",     2, 1 },
147bf215546Sopenharmony_ci   { OPCODE_SWZ,    "SWZ",     1, 1 },
148bf215546Sopenharmony_ci   { OPCODE_TEX,    "TEX",     1, 1 },
149bf215546Sopenharmony_ci   { OPCODE_TXB,    "TXB",     1, 1 },
150bf215546Sopenharmony_ci   { OPCODE_TXD,    "TXD",     3, 1 },
151bf215546Sopenharmony_ci   { OPCODE_TXL,    "TXL",     1, 1 },
152bf215546Sopenharmony_ci   { OPCODE_TXP,    "TXP",     1, 1 },
153bf215546Sopenharmony_ci   { OPCODE_TRUNC,  "TRUNC",   1, 1 },
154bf215546Sopenharmony_ci   { OPCODE_XPD,    "XPD",     2, 1 }
155bf215546Sopenharmony_ci};
156bf215546Sopenharmony_ci
157bf215546Sopenharmony_ci
158bf215546Sopenharmony_ci/**
159bf215546Sopenharmony_ci * Return the number of src registers for the given instruction/opcode.
160bf215546Sopenharmony_ci */
161bf215546Sopenharmony_ciGLuint
162bf215546Sopenharmony_ci_mesa_num_inst_src_regs(enum prog_opcode opcode)
163bf215546Sopenharmony_ci{
164bf215546Sopenharmony_ci   assert(opcode < MAX_OPCODE);
165bf215546Sopenharmony_ci   assert(opcode == InstInfo[opcode].Opcode);
166bf215546Sopenharmony_ci   assert(OPCODE_XPD == InstInfo[OPCODE_XPD].Opcode);
167bf215546Sopenharmony_ci   return InstInfo[opcode].NumSrcRegs;
168bf215546Sopenharmony_ci}
169bf215546Sopenharmony_ci
170bf215546Sopenharmony_ci
171bf215546Sopenharmony_ci/**
172bf215546Sopenharmony_ci * Return the number of dst registers for the given instruction/opcode.
173bf215546Sopenharmony_ci */
174bf215546Sopenharmony_ciGLuint
175bf215546Sopenharmony_ci_mesa_num_inst_dst_regs(enum prog_opcode opcode)
176bf215546Sopenharmony_ci{
177bf215546Sopenharmony_ci   assert(opcode < MAX_OPCODE);
178bf215546Sopenharmony_ci   assert(opcode == InstInfo[opcode].Opcode);
179bf215546Sopenharmony_ci   assert(OPCODE_XPD == InstInfo[OPCODE_XPD].Opcode);
180bf215546Sopenharmony_ci   return InstInfo[opcode].NumDstRegs;
181bf215546Sopenharmony_ci}
182bf215546Sopenharmony_ci
183bf215546Sopenharmony_ci
184bf215546Sopenharmony_ci/**
185bf215546Sopenharmony_ci * Return string name for given program opcode.
186bf215546Sopenharmony_ci */
187bf215546Sopenharmony_ciconst char *
188bf215546Sopenharmony_ci_mesa_opcode_string(enum prog_opcode opcode)
189bf215546Sopenharmony_ci{
190bf215546Sopenharmony_ci   if (opcode < MAX_OPCODE)
191bf215546Sopenharmony_ci      return InstInfo[opcode].Name;
192bf215546Sopenharmony_ci   else {
193bf215546Sopenharmony_ci      static char s[20];
194bf215546Sopenharmony_ci      snprintf(s, sizeof(s), "OP%u", opcode);
195bf215546Sopenharmony_ci      return s;
196bf215546Sopenharmony_ci   }
197bf215546Sopenharmony_ci}
198bf215546Sopenharmony_ci
199