File: orcexecutor.c

package info (click to toggle)
orc 1:0.4.6-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 3,856 kB
  • ctags: 2,889
  • sloc: ansic: 29,000; sh: 10,220; xml: 2,508; makefile: 196
file content (248 lines) | stat: -rw-r--r-- 6,330 bytes parent folder | download
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

#include "config.h"

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <orc/orcprogram.h>
#include <orc/orcdebug.h>

/**
 * SECTION:orcexecutor
 * @title: OrcExecutor
 * @short_description: Running Orc programs
 */


OrcExecutor *
orc_executor_new (OrcProgram *program)
{
  OrcExecutor *ex;

  ex = malloc(sizeof(OrcExecutor));
  memset(ex,0,sizeof(OrcExecutor));

  ex->program = program;

  return ex;
}

void
orc_executor_free (OrcExecutor *ex)
{
  free (ex);
}

void
orc_executor_run (OrcExecutor *ex)
{
  void (*func) (OrcExecutor *);

  func = ex->program->code_exec;
  if (func) {
    func (ex);
    //ORC_ERROR("counters %d %d %d", ex->counter1, ex->counter2, ex->counter3);
  } else {
    orc_executor_emulate (ex);
  }
}

void
orc_executor_set_program (OrcExecutor *ex, OrcProgram *program)
{
  ex->program = program;
  if (program->code_exec) {
    ex->arrays[ORC_VAR_A1] = (void *)program->code_exec;
  } else {
    ex->arrays[ORC_VAR_A1] = (void *)orc_executor_emulate;
  }
}

void
orc_executor_set_array (OrcExecutor *ex, int var, void *ptr)
{
  ex->arrays[var] = ptr;
}

void
orc_executor_set_stride (OrcExecutor *ex, int var, int stride)
{
  ex->params[var] = stride;
}

void
orc_executor_set_array_str (OrcExecutor *ex, const char *name, void *ptr)
{
  int var;
  var = orc_program_find_var_by_name (ex->program, name);
  ex->arrays[var] = ptr;
}

void
orc_executor_set_param (OrcExecutor *ex, int var, int value)
{
  ex->params[var] = value;
}

void
orc_executor_set_param_str (OrcExecutor *ex, const char *name, int value)
{
  int var;
  var = orc_program_find_var_by_name (ex->program, name);
  ex->params[var] = value;
}

int
orc_executor_get_accumulator (OrcExecutor *ex, int var)
{
  return ex->accumulators[var - ORC_VAR_A1];
}

int
orc_executor_get_accumulator_str (OrcExecutor *ex, const char *name)
{
  int var;
  var = orc_program_find_var_by_name (ex->program, name);
  return ex->accumulators[var];
}

void
orc_executor_set_n (OrcExecutor *ex, int n)
{
  ex->n = n;
}

void
orc_executor_set_m (OrcExecutor *ex, int m)
{
  ORC_EXECUTOR_M(ex) = m;
}

void
orc_executor_emulate (OrcExecutor *ex)
{
  int i;
  int j;
  int k;
  int m, m_index;
  OrcProgram *program = ex->program;
  OrcInstruction *insn;
  OrcStaticOpcode *opcode;
  OrcOpcodeExecutor opcode_ex;

  ex->accumulators[0] = 0;
  ex->accumulators[1] = 0;
  ex->accumulators[2] = 0;
  ex->accumulators[3] = 0;

  ORC_DEBUG("emulating");

  memset (&opcode_ex, 0, sizeof(opcode_ex));

  if (program->is_2d) {
    m = ORC_EXECUTOR_M(ex);
  } else {
    m = 1;
  }
  ORC_DEBUG("src ptr %p stride %d", ex->arrays[ORC_VAR_S1], ex->params[ORC_VAR_S1]);
  for(m_index=0;m_index<m;m_index++){
    ORC_DEBUG("m_index %d m %d", m_index, m);
    for(i=0;i<ex->n;i++){
      for(j=0;j<program->n_insns;j++){
        insn = program->insns + j;
        opcode = insn->opcode;

        /* set up args */
        for(k=0;k<ORC_STATIC_OPCODE_N_SRC;k++) {
          OrcVariable *var = program->vars + insn->src_args[k];

          if (opcode->src_size[k] == 0) continue;

          if (var->vartype == ORC_VAR_TYPE_CONST) {
            opcode_ex.src_values[k] = var->value;
          } else if (var->vartype == ORC_VAR_TYPE_PARAM) {
            opcode_ex.src_values[k] = ex->params[insn->src_args[k]];
          } else if (var->vartype == ORC_VAR_TYPE_TEMP) {
            /* FIXME shouldn't store executor stuff in program */
            opcode_ex.src_values[k] = var->value;
          } else if (var->vartype == ORC_VAR_TYPE_SRC ||
              var->vartype == ORC_VAR_TYPE_DEST) {
            void *ptr = ORC_PTR_OFFSET(ex->arrays[insn->src_args[k]],
                var->size*i + ex->params[insn->src_args[k]]*m_index);

            switch (var->size) {
              case 1:
                opcode_ex.src_values[k] = *(orc_int8 *)ptr;
                break;
              case 2:
                opcode_ex.src_values[k] = *(orc_int16 *)ptr;
                break;
              case 4:
                opcode_ex.src_values[k] = *(orc_int32 *)ptr;
                break;
              case 8:
                opcode_ex.src_values[k] = *(orc_int64 *)ptr;
                break;
              default:
                ORC_ERROR("unhandled size %d", program->vars[insn->src_args[k]].size);
            }
          } else {
            ORC_ERROR("shouldn't be reached (%d)", var->vartype);
          }
        }

        opcode->emulate (&opcode_ex, opcode->emulate_user);

        for(k=0;k<ORC_STATIC_OPCODE_N_DEST;k++){
          OrcVariable *var = program->vars + insn->dest_args[k];

          if (opcode->dest_size[k] == 0) continue;

          if (var->vartype == ORC_VAR_TYPE_TEMP) {
            /* FIXME shouldn't store executor stuff in program */
            var->value = opcode_ex.dest_values[k];
          } else if (var->vartype == ORC_VAR_TYPE_DEST) {
            void *ptr = ORC_PTR_OFFSET(ex->arrays[insn->dest_args[k]],
                var->size*i + ex->params[insn->dest_args[k]]*m_index);

            switch (var->size) {
              case 1:
                *(orc_int8 *)ptr = opcode_ex.dest_values[k];
                break;
              case 2:
                *(orc_int16 *)ptr = opcode_ex.dest_values[k];
                break;
              case 4:
                *(orc_int32 *)ptr = opcode_ex.dest_values[k];
                break;
              case 8:
                *(orc_int64 *)ptr = opcode_ex.dest_values[k];
                break;
              default:
                ORC_ERROR("unhandled size %d", program->vars[insn->dest_args[k]].size);
            }
          } else if (var->vartype == ORC_VAR_TYPE_ACCUMULATOR) {
            switch (var->size) {
              case 2:
                ex->accumulators[insn->dest_args[k] - ORC_VAR_A1] +=
                  opcode_ex.dest_values[k];
                ex->accumulators[insn->dest_args[k] - ORC_VAR_A1] &= 0xffff;
                break;
              case 4:
                ex->accumulators[insn->dest_args[k] - ORC_VAR_A1] +=
                  opcode_ex.dest_values[k];
                break;
              default:
                ORC_ERROR("unhandled size %d", program->vars[insn->dest_args[k]].size);
            }
          } else {
            ORC_ERROR("shouldn't be reached (%d)", var->vartype);
          }
        }
      }
    }
  }
}