summaryrefslogtreecommitdiff
path: root/misc/pascal/insn32/regm/regm.c
blob: eefc5e9530706c2b582c50bad5ad2425f97d5ee3 (plain) (blame)
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
/**********************************************************************
 * regm.c
 * Convert 32-bit pcode defintions to a register model
 *
 *   Copyright (C) 2008 Gregory Nutt. All rights reserved.
 *   Author: Gregory Nutt <spudmonkey@racsa.co.cr>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 3. Neither the name NuttX nor the names of its contributors may be
 *    used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 **********************************************************************/

/**********************************************************************
 * Included Files
 **********************************************************************/

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

#include "keywords.h"
#include "pdefs.h"
#include "pedefs.h"
#include "paslib.h"
#include "pofflib.h"
#include "perr.h"

#include "regm.h"
#include "regm_tree.h"
#include "regm_pass1.h"
#include "regm_pass2.h"

/**********************************************************************
 * Private Function Prototypes
 **********************************************************************/

static void         regm_ShowUsage(const char *progname, int errcode);
static int          regm_CheckPoffFile(poffHandle_t hPoff);
static poffHandle_t regm_ReadPoffFile(const char *filename);
static void         regm_Pass3(poffHandle_t hPoff);
static void         regm_Pass4(poffHandle_t hPoff);
static void         regm_Pass5(poffHandle_t hPoff);
static void         regm_WritePoffFile(poffHandle_t hPoff,
				       const char *filename);

/**********************************************************************
 * Public Variables
 **********************************************************************/

int vRegmDebug = 0;

/**********************************************************************
 * Private Variables
 **********************************************************************/

/**********************************************************************
 * Private Functions
 **********************************************************************/

/***********************************************************************/

static void regm_ShowUsage(const char *progname, int errcode)
{
  fprintf(stderr, "USAGE:\n");
  fprintf(stderr, "  %s -h\n", progname);
  fprintf(stderr, "  %s [-d] <poff-filename>\n", progname);
  fprintf(stderr, "WHERE:\n");
  fprintf(stderr, "  -d:  Enables debug output\n");
  fprintf(stderr, "  -h:  Shows this message\n");
  exit(errcode);
}

/***********************************************************************/

static poffHandle_t regm_ReadPoffFile(const char *filename)
{
  poffHandle_t hPoff;
  char    objname [FNAME_SIZE+1];
  FILE   *objFile;
  int     errcode;

  TRACE(stderr, "[regm_ReadPoffFile]");

  /* Open the optimized POFF object file -- Use the .o extension */

  (void)extension(filename, "o", objname, 1);
  if (!(objFile = fopen(objname, "rb")))
    {
      printf("Error Opening %s\n", objname);
      exit(1);
    }

  /* Get a handle to a POFF input object */

  hPoff = poffCreateHandle();
  if (!hPoff)
    {
      printf("Could not get POFF handle\n");
      exit(1);
    }

  /* Read the POFF file into memory */

  errcode = poffReadFile(hPoff, objFile);
  if (errcode != 0)
    {
      printf("Could not read POFF file, errcode=0x%02x\n", errcode);
      exit(1);
    }

  /* Close the input file */

  fclose(objFile);
  return hPoff;
}

/***********************************************************************/

static int regm_CheckPoffFile(poffHandle_t hPoff)
{
  ubyte fileArch = poffGetArchitecture(hPoff);
  if (fileArch != FHA_PCODE_INSN32)
    {
      fprintf(stderr, "ERROR: File is not 32-bit pcode (%d)\n",
	      fileArch);
      return -1;
    }
  return 0;
}

/***********************************************************************/
/* Pass3: Perform local optimization */

static void regm_Pass3(poffHandle_t hPoff)
{
  TRACE(stderr, "[regm_Pass3]");
}

/***********************************************************************/
/* Pass 4: Fixup register usage, force to use a fixed number of registers
 * (arguments, static registers, volatile registers, special registers),
 * and add logic to handle large immediate values.
 */

static void regm_Pass4(poffHandle_t hPoff)
{
  TRACE(stderr, "[regm_Pass4]");
}

/***********************************************************************/
/* Pass 5: Fixup BL and B offsets, section headers, relocation entries,
 * symbol values
 */

static void regm_Pass5(poffHandle_t hPoff)
{
  TRACE(stderr, "[regm_Pass5]");
}

/***********************************************************************/

static void regm_WritePoffFile(poffHandle_t hPoff, const char *filename)
{
#if 0
  char    rexname [FNAME_SIZE+1];
  FILE   *rexFile;

  TRACE(stderr, "[regm_WritePoffFile]");

  /* Open optimized p-code file -- Use .o extension */

  (void)extension(filename, ".rex", rexname, 1);
  if (!(rexFile = fopen(rexname, "wb")))
    {
      printf("Error Opening %s\n", rexname);
      exit(1);
    }

  /* Then write the new POFF file */

  poffWritePoffFile(hPoff, rexFile);

  /* Destroy the POFF object */

  poffDestroyHandle(hPoff);

  /* Close the files used on regm_WritePoffFile */

  (void)fclose(rexFile);
#endif
}

/**********************************************************************
 * Public Functions
 **********************************************************************/

/***********************************************************************/

int main(int argc, char *argv[], char *envp[])
{
  const char *outfilename;
  poffHandle_t hPoff;
  TRACE(stderr, "[main]");
  int option;

  /* Process command line argruments */

  while ((option = getopt(argc, argv, "dh")) > 0)
    {
      switch (option)
	{
	case 'd' :
	  vRegmDebug++;
	  break;
	case 'h' :
	  regm_ShowUsage(argv[0], 0);
	default:
	  fprintf(stderr, "Unrecognized option\n");
	  regm_ShowUsage(argv[0], -1);
	}
    }

  /* Check for existence of filename argument */

  if (optind != argc - 1)
    {
      fprintf(stderr, "Filename required at end of command line.\n");
      regm_ShowUsage(argv[0], -1);
    }

  /* Read the POFF file into memory */

  outfilename = argv[optind];
  hPoff = regm_ReadPoffFile(outfilename);

  /* Verify that it is the kind of file that we can handle */

  if (regm_CheckPoffFile(hPoff) != 0)
    {
      fprintf(stderr, "File is not the correct type\n");
      regm_ShowUsage(argv[0], -1);
    }

  /* Read the debug info into a more usable structure.  And release
   * the debug info in the POFF object (we will be replacing it later)
   */

  poffReadDebugFuncInfoTable(hPoff);
  poffDiscardDebugFuncInfo(hPoff);

  /* Pass 1: Break the POFF program data into sections and buffer in
   * a tree structure.
   */

  regm_InitTree();
  regm_Pass1(hPoff);
  regm_DumpTree();

  /* We can eliminate the buffered pcode data because we have
   * a copy in the tree and we will be replacing it before we
   * save the file.
   */

  poffReleaseProgData(hPoff);

  /* Pass 2: Convert the buffered pcode to the basic register model
   * with an indefinite number of registers (arguments, general, and
   * special registers) and with 32-bit immediate size.
   */

  regm_Pass2(hPoff);

  /* Pass3: Perform local optimization */

  regm_Pass3(hPoff);

  /* Pass 4: Fixup register usage, force to use a fixed number of registers
   * (arguments, static registers, volatile registers, special registers),
   * and add logic to handle large immediate values.
   */

  regm_Pass4(hPoff);

  /* Pass 5: Fixup BL and B offsets, section headers, relocation entries,
   * symbol values
   */

  regm_Pass5(hPoff);

  /* Replace the debug info in the POFF object with the debug info
   * we have modified.  We no longer need the buffered debug info after
   * this point.
   */

  poffReplaceDebugFuncInfo(hPoff);
  poffReleaseDebugFuncInfoTable();

  /* Write the REGM POFF file */

  poffSetArchitecture(hPoff, FHA_REGM_INSN32);
  regm_WritePoffFile(hPoff, outfilename);
  return 0;
}

/***********************************************************************/

void regm_ProgSeek(poffHandle_t hPoff, uint32 dwOffset)
{
  insn_ResetOpCodeRead(hPoff);
  if (poffProgSeek(hPoff, dwOffset) < 0)
    {
      fatal(eSEEKFAIL);
    }
}

/***********************************************************************/