PageRenderTime 2ms CodeModel.GetById 2ms app.highlight 22ms RepoModel.GetById 1ms app.codeStats 1ms

/misc/pascal/insn16/popt/pjopt.c

https://github.com/markangelo/PX4NuttX
C | 450 lines | 342 code | 67 blank | 41 comment | 16 complexity | 07c93fc34d1b2e8ceca58ee66cf7ecdf MD5 | raw file
  1/**********************************************************************
  2 *  pjopt.c
  3 *  Branch Optimizations
  4 *
  5 *   Copyright (C) 2008-2009 Gregory Nutt. All rights reserved.
  6 *   Author: Gregory Nutt <gnutt@nuttx.org>
  7 *
  8 * Redistribution and use in source and binary forms, with or without
  9 * modification, are permitted provided that the following conditions
 10 * are met:
 11 *
 12 * 1. Redistributions of source code must retain the above copyright
 13 *    notice, this list of conditions and the following disclaimer.
 14 * 2. Redistributions in binary form must reproduce the above copyright
 15 *    notice, this list of conditions and the following disclaimer in
 16 *    the documentation and/or other materials provided with the
 17 *    distribution.
 18 * 3. Neither the name NuttX nor the names of its contributors may be
 19 *    used to endorse or promote products derived from this software
 20 *    without specific prior written permission.
 21 *
 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 26 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 29 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 32 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 33 * POSSIBILITY OF SUCH DAMAGE.
 34 *
 35 **********************************************************************/
 36
 37/**********************************************************************
 38 * Included Files
 39 **********************************************************************/
 40
 41#include <stdint.h>
 42#include <stdio.h>
 43
 44#include "keywords.h"
 45#include "pdefs.h"
 46#include "pinsn16.h"
 47
 48#include "popt.h"
 49#include "polocal.h"
 50#include "pjopt.h"
 51
 52/**********************************************************************/
 53
 54int16_t BranchOptimize (void)
 55{
 56  int16_t nchanges = 0;
 57  register int16_t i;
 58
 59  TRACE(stderr, "[BranchOptimize]");
 60
 61  /* At least two pcodes are need to perform branch optimizations */
 62
 63  i = 0;
 64  while (i < nops-1)
 65    {
 66      switch (pptr[i]->op)
 67        {
 68        case oNOT :
 69          switch (pptr[i+1]->op)
 70            {
 71            case oJEQUZ :
 72              pptr[i+1]->op = oJNEQZ;
 73              deletePcode(i);
 74              nchanges++;
 75              break;
 76
 77            case oJNEQZ :
 78              pptr[i+1]->op = oJEQUZ;
 79              deletePcode(i);
 80              nchanges++;
 81              break;
 82
 83            default     :
 84              i++;
 85              break;
 86            } /* end switch */
 87          break;
 88
 89        case oNEG :
 90          switch (pptr[i+1]->op)
 91            {
 92            case oJLTZ  :
 93              pptr[i+1]->op = oJGTZ;
 94              deletePcode(i);
 95              nchanges++;
 96              break;
 97
 98            case oJGTEZ :
 99              pptr[i+1]->op = oJLTEZ;
100              deletePcode(i);
101              nchanges++;
102              break;
103
104            case oJGTZ  :
105              pptr[i+1]->op = oJLTZ;
106              deletePcode(i);
107              nchanges++;
108              break;
109
110            case oJLTEZ :
111              pptr[i+1]->op = oJGTEZ;
112              deletePcode(i);
113              nchanges++;
114              break;
115
116            default     :
117              i++;
118              break;
119            } /* end switch */
120          break;
121
122        case oEQU  :
123          switch (pptr[i+1]->op)
124            {
125            case oNOT :
126              pptr[i]->op = oNEQ;
127              deletePcode(i+1);
128              nchanges++;
129              break;
130
131            case oJEQUZ :
132              pptr[i+1]->op = oJNEQ;
133              deletePcode(i);
134              nchanges++;
135              break;
136
137            case oJNEQZ :
138              pptr[i+1]->op = oJEQU;
139              deletePcode(i);
140              nchanges++;
141              break;
142
143            default     :
144              i++;
145              break;
146            } /* end switch */
147          break;
148
149        case oNEQ  :
150          switch (pptr[i+1]->op)
151            {
152            case oNOT :
153              pptr[i]->op = oEQU;
154              deletePcode(i+1);
155              nchanges++;
156              break;
157
158            case oJEQUZ :
159              pptr[i+1]->op = oJEQU;
160              deletePcode(i);
161              nchanges++;
162              break;
163
164            case oJNEQZ :
165              pptr[i+1]->op = oJNEQ;
166              deletePcode(i);
167              nchanges++;
168              break;
169
170            default     :
171              i++;
172              break;
173            } /* end switch */
174          break;
175
176        case oLT   :
177          switch (pptr[i+1]->op)
178            {
179            case oNOT :
180              pptr[i]->op = oGTE;
181              deletePcode(i+1);
182              nchanges++;
183              break;
184
185            case oJEQUZ :
186              pptr[i+1]->op = oJGTE;
187              deletePcode(i);
188              nchanges++;
189              break;
190
191            case oJNEQZ :
192              pptr[i+1]->op = oJLT;
193              deletePcode(i);
194              nchanges++;
195              break;
196
197            default     :
198              i++;
199              break;
200            } /* end switch */
201          break;
202
203        case oGTE  :
204          switch (pptr[i+1]->op)
205            {
206            case oNOT :
207              pptr[i]->op = oLT;
208              deletePcode(i+1);
209              nchanges++;
210              break;
211
212            case oJEQUZ :
213              pptr[i+1]->op = oJLT;
214              deletePcode(i);
215              nchanges++;
216              break;
217
218            case oJNEQZ :
219              pptr[i+1]->op = oJGTE;
220              deletePcode(i);
221              nchanges++;
222              break;
223
224            default     :
225              i++;
226              break;
227            } /* end switch */
228          break;
229
230        case oGT   :
231          switch (pptr[i+1]->op)
232            {
233            case oNOT :
234              pptr[i]->op = oLTE;
235              deletePcode(i+1);
236              nchanges++;
237              break;
238
239            case oJEQUZ :
240              pptr[i+1]->op = oJLTE;
241              deletePcode(i);
242              nchanges++;
243              break;
244
245            case oJNEQZ :
246              pptr[i+1]->op = oJGT;
247              deletePcode(i);
248              nchanges++;
249              break;
250
251            default     :
252              i++;
253              break;
254            } /* end switch */
255          break;
256
257        case oLTE :
258          switch (pptr[i+1]->op)
259            {
260            case oNOT :
261              pptr[i]->op = oGT;
262              deletePcode(i+1);
263              nchanges++;
264              break;
265
266            case oJEQUZ :
267              pptr[i+1]->op = oJGT;
268              deletePcode(i);
269              nchanges++;
270              break;
271
272            case oJNEQZ :
273              pptr[i+1]->op = oJLTE;
274              deletePcode(i);
275              nchanges++;
276              break;
277
278            default     :
279              i++;
280              break;
281            } /* end switch */
282          break;
283
284        case oEQUZ  :
285          switch (pptr[i+1]->op)
286            {
287            case oNOT :
288              pptr[i]->op = oNEQZ;
289              deletePcode(i+1);
290              nchanges++;
291              break;
292
293            case oJEQUZ :
294              pptr[i+1]->op = oJNEQZ;
295              deletePcode(i);
296              nchanges++;
297              break;
298
299            case oJNEQZ :
300              pptr[i+1]->op = oJEQUZ;
301              deletePcode(i);
302              nchanges++;
303              break;
304
305            default     :
306              i++;
307              break;
308            } /* end switch */
309          break;
310
311        case oNEQZ  :
312          switch (pptr[i+1]->op)
313            {
314            case oNOT :
315              pptr[i]->op = oEQUZ;
316              deletePcode(i+1);
317              nchanges++;
318              break;
319
320            case oJEQUZ :
321            case oJNEQZ :
322              deletePcode(i);
323              nchanges++;
324              break;
325
326            default     :
327              i++;
328              break;
329            } /* end switch */
330          break;
331
332        case oLTZ   :
333          switch (pptr[i+1]->op)
334            {
335            case oNOT :
336              pptr[i]->op = oGTEZ;
337              deletePcode(i+1);
338              nchanges++;
339              break;
340
341            case oJEQUZ :
342              pptr[i+1]->op = oJGTEZ;
343              deletePcode(i);
344              nchanges++;
345              break;
346
347            case oJNEQZ :
348              pptr[i+1]->op = oJLTZ;
349              deletePcode(i);
350              nchanges++;
351              break;
352
353            default     :
354              i++;
355              break;
356            } /* end switch */
357          break;
358
359        case oGTEZ  :
360          switch (pptr[i+1]->op)
361            {
362            case oNOT :
363              pptr[i]->op = oLTZ;
364              deletePcode(i+1);
365              nchanges++;
366              break;
367
368            case oJEQUZ :
369              pptr[i+1]->op = oJLTZ;
370              deletePcode(i);
371              nchanges++;
372              break;
373
374            case oJNEQZ :
375              pptr[i+1]->op = oJGTEZ;
376              deletePcode(i);
377              nchanges++;
378              break;
379
380            default     :
381              i++;
382              break;
383            } /* end switch */
384          break;
385
386        case oGTZ   :
387          switch (pptr[i+1]->op)
388            {
389            case oNOT :
390              pptr[i]->op = oLTEZ;
391              deletePcode(i+1);
392              nchanges++;
393              break;
394
395            case oJEQUZ :
396              pptr[i+1]->op = oJLTEZ;
397              deletePcode(i);
398              nchanges++;
399              break;
400
401            case oJNEQZ :
402              pptr[i+1]->op = oJGTZ;
403              deletePcode(i);
404              nchanges++;
405              break;
406
407            default     :
408              i++;
409              break;
410            } /* end switch */
411          break;
412
413        case oLTEZ :
414          switch (pptr[i+1]->op)
415            {
416            case oNOT :
417              pptr[i]->op = oGTZ;
418              deletePcode(i+1);
419              nchanges++;
420              break;
421
422            case oJEQUZ :
423              pptr[i+1]->op = oJGTZ;
424              deletePcode(i);
425              nchanges++;
426              break;
427
428            case oJNEQZ :
429              pptr[i+1]->op = oJLTEZ;
430              deletePcode(i);
431              nchanges++;
432              break;
433
434            default     :
435              i++;
436              break;
437            } /* end switch */
438          break;
439
440        default     :
441          i++;
442          break;
443        } /* end switch */
444    } /* end while */
445  return (nchanges);
446
447} /* end BranchOptimize */
448
449/**********************************************************************/
450