summaryrefslogtreecommitdiff
path: root/misc/pascal/insn16/popt/polocal.c
blob: c2b2a5b679934490c2768bbb7c83cb6fd83585ca (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
/**********************************************************************
 * polocal.c
 * P-Code Local Optimizer
 *
 *   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 "keywords.h"
#include "podefs.h"
#include "pinsn16.h"

#include "pofflib.h"
#include "paslib.h"
#include "pinsn.h"
#include "pcopt.h"
#include "plopt.h"
#include "pjopt.h"
#include "polocal.h"

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

static void initPTable        (void);
static void putPCodeFromTable (void);
static void setupPointer      (void);

/**********************************************************************
 * Global Variables
 **********************************************************************/

OPTYPE  ptable [WINDOW];                /* Pcode Table */
OPTYPE *pptr   [WINDOW];                /* Valid Pcode Pointers */

sint16  nops    = 0;                    /* No. Valid Pcode Pointers */
sint16  end_out = 0;                    /* 1 = oEND pcode has been output */

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

static poffHandle_t     myPoffHandle;         /* Handle to POFF object */
static poffProgHandle_t myPoffProgHandle;/* Handle to temporary POFF object */

/**********************************************************************
 * Global Functions
 **********************************************************************/

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

void localOptimization(poffHandle_t poffHandle,
		       poffProgHandle_t poffProgHandle)
{
  sint16 nchanges;

  TRACE(stderr, "[pass2]");

  /* Save the handles for use by other, private functions */

  myPoffHandle     = poffHandle;
  myPoffProgHandle = poffProgHandle;

  /* Initialization */

  initPTable();

  /* Outer loop traverse the file op-code by op-code until the oEND P-Code
   * has been output.  NOTE:  it is assumed throughout that oEND is the 
   * final P-Code in the program data section.
   */

  while (!(end_out))
    {
      /* The inner loop optimizes the buffered P-Codes until no further
       * changes can be made.  Then the outer loop will advance the buffer
       * by one P-Code
       */

      do
	{
	  nchanges  = unaryOptimize ();
	  nchanges += binaryOptimize();
	  nchanges += BranchOptimize();
	  nchanges += LoadOptimize();
	  nchanges += StoreOptimize();
	} while (nchanges);

      putPCodeFromTable();
    }
}

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

void deletePcode(sint16 delIndex)
{
  TRACE(stderr, "[deletePcode]");

  pptr[delIndex]->op   = oNOP;
  pptr[delIndex]->arg1 = 0;
  pptr[delIndex]->arg2 = 0;
  setupPointer();
}

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

void deletePcodePair(sint16 delIndex1, sint16 delIndex2)
{
  TRACE(stderr, "[deletePcodePair]");

  pptr[delIndex1]->op   = oNOP;
  pptr[delIndex1]->arg1 = 0;
  pptr[delIndex1]->arg2 = 0;
  pptr[delIndex2]->op   = oNOP;
  pptr[delIndex2]->arg1 = 0;
  pptr[delIndex2]->arg2 = 0;
  setupPointer();
}

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

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

static void putPCodeFromTable(void)
{
  register sint16 i;

  TRACE(stderr, "[putPCodeFromTable]");

  /* Transfer all buffered P-Codes (except NOPs) to the optimized file */
  do
    {
      if ((ptable[0].op != oNOP) && !(end_out))
	{
	  (void)poffAddTmpProgByte(myPoffProgHandle, ptable[0].op);

	  if (ptable[0].op & o8)  
	    (void)poffAddTmpProgByte(myPoffProgHandle, ptable[0].arg1);

	  if (ptable[0].op & o16)
	    { 
	      (void)poffAddTmpProgByte(myPoffProgHandle,
				       (ptable[0].arg2 >> 8));
	      (void)poffAddTmpProgByte(myPoffProgHandle,
				       (ptable[0].arg2 & 0xff));
	    }

	  end_out =(ptable[0].op == oEND);
	}

      /* Move all P-Codes down one slot */

      for (i = 1; i < WINDOW; i++)
	{
	  ptable[i-1].op   = ptable[i].op ;
	  ptable[i-1].arg1 = ptable[i].arg1;
	  ptable[i-1].arg2 = ptable[i].arg2;
	}

      /* Then fill the end slot with a new P-Code from the input file */

      insn_GetOpCode(myPoffHandle, &ptable[WINDOW-1]);

    } while (ptable[0].op == oNOP);
  setupPointer();
}

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

static void setupPointer(void)
{
  register sint16 pindex;

  TRACE(stderr, "[setupPointer]");

  for (pindex = 0; pindex < WINDOW; pindex++)
    pptr[pindex] = (OPTYPE *) NULL;

  nops = 0; 
  for (pindex = 0; pindex < WINDOW; pindex++)
    {
      switch (ptable[pindex].op)
	{
	  /* Terminate list when a break from sequential logic is
	   * encountered
	   */

	case oRET   :
	case oEND   :
	case oJMP   :
	case oLABEL :
	case oPCAL  :
	  return;

	  /* Terminate list when a condition break from sequential logic is
	   * encountered but include the conditional branch in the list
	   */

	case oJEQUZ :
	case oJNEQZ :
	case oJLTZ  :
	case oJGTEZ :
	case oJGTZ  :
	case oJLTEZ :
	  pptr[nops] = &ptable[pindex];
	  nops++;
	  return;

	  /* Skip over NOPs and comment class pcodes */

	case oNOP   :
	case oLINE  :
	  break;

	  /* Include all other pcodes in the optimization list and continue */

	default     :
	  pptr[nops] = &ptable[pindex];
	  nops++;
	}
    }
}

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

static void initPTable(void)
{
  register sint16 i;

  TRACE(stderr, "[intPTable]");

  /* Skip over leading pcodes.  NOTE:  assumes executable begins after
   * the first oLABEL pcode
   */

  do
    {
      insn_GetOpCode(myPoffHandle, &ptable[0]);

      (void)poffAddTmpProgByte(myPoffProgHandle, ptable[0].op);

      if (ptable[0].op & o8)  
	{
	  (void)poffAddTmpProgByte(myPoffProgHandle, ptable[0].arg1);
	}

      if (ptable[0].op & o16)
	{ 
	  (void)poffAddTmpProgByte(myPoffProgHandle, (ptable[0].arg2 >> 8));
	  (void)poffAddTmpProgByte(myPoffProgHandle, (ptable[0].arg2 & 0xff));
	} /* end if */
    }
  while ((ptable[0].op != oLABEL) && (ptable[0].op != oEND));

  /* Fill the pcode window and setup pointers to working section */

  for (i = 0; i < WINDOW; i++)
    {
      insn_GetOpCode(myPoffHandle, &ptable[i]);
    }
  setupPointer();
}

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