diff options
author | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2009-12-18 15:43:29 +0000 |
---|---|---|
committer | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2009-12-18 15:43:29 +0000 |
commit | eb3074a4fe3b7c86d2a3ca139eb881964f2f23e1 (patch) | |
tree | 5ce1e76df21acb8d0b5b209fe7f2461a78c36a8e /misc/pascal/insn16/popt | |
parent | af6cfa901fa8cc78607976e8ed8aedc195500aff (diff) | |
download | nuttx-eb3074a4fe3b7c86d2a3ca139eb881964f2f23e1.tar.gz nuttx-eb3074a4fe3b7c86d2a3ca139eb881964f2f23e1.tar.bz2 nuttx-eb3074a4fe3b7c86d2a3ca139eb881964f2f23e1.zip |
Update to use stdint/stdbool.h
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@2383 42af7a65-404d-4744-a932-0658087f49c3
Diffstat (limited to 'misc/pascal/insn16/popt')
-rw-r--r-- | misc/pascal/insn16/popt/pcopt.c | 1811 | ||||
-rw-r--r-- | misc/pascal/insn16/popt/pcopt.h | 100 | ||||
-rw-r--r-- | misc/pascal/insn16/popt/pfopt.c | 383 | ||||
-rw-r--r-- | misc/pascal/insn16/popt/pjopt.c | 899 | ||||
-rw-r--r-- | misc/pascal/insn16/popt/pjopt.h | 92 | ||||
-rw-r--r-- | misc/pascal/insn16/popt/plopt.c | 499 | ||||
-rw-r--r-- | misc/pascal/insn16/popt/plopt.h | 98 | ||||
-rw-r--r-- | misc/pascal/insn16/popt/polocal.c | 601 | ||||
-rw-r--r-- | misc/pascal/insn16/popt/polocal.h | 147 | ||||
-rw-r--r-- | misc/pascal/insn16/popt/popt.c | 577 | ||||
-rw-r--r-- | misc/pascal/insn16/popt/psopt.c | 281 |
11 files changed, 2761 insertions, 2727 deletions
diff --git a/misc/pascal/insn16/popt/pcopt.c b/misc/pascal/insn16/popt/pcopt.c index 5af19002c..5f8deed80 100644 --- a/misc/pascal/insn16/popt/pcopt.c +++ b/misc/pascal/insn16/popt/pcopt.c @@ -1,905 +1,906 @@ -/**********************************************************************
- * pcopt.c
- * Constant Expression Optimizations
- *
- * 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 "pdefs.h"
-#include "pinsn16.h"
-
-#include "paslib.h"
-#include "popt.h"
-#include "polocal.h"
-#include "pcopt.h"
-
-/**********************************************************************/
-
-sint16 unaryOptimize(void)
-{
- sint16 nchanges = 0;
- register uint16 temp;
- register sint16 i;
-
- TRACE(stderr, "[unaryOptimize]");
-
- /* At least two pcodes are need to perform unary optimizations */
-
- i = 0;
- while (i < nops-1)
- {
- /* Check for a constant value being pushed onto the stack */
-
- if ((pptr[i]->op == oPUSH) || (pptr[i]->op == oPUSHB))
- {
- /* Turn the oPUSHB into an oPUSH op (temporarily) */
-
- if (pptr[i]->op == oPUSHB)
- {
- pptr[i]->op = oPUSH;
- pptr[i]->arg2 = pptr[i]->arg1;
- pptr[i]->arg1 = 0;
- } /* end if */
-
- switch (pptr[i+1]->op)
- {
- /* Delete unary operators on constants */
- case oNEG :
- pptr[i]->arg2 = -(pptr[i]->arg2);
- deletePcode(i+1);
- nchanges++;
- break;
-
- case oABS :
- if (signExtend16(pptr[i]->arg2) < 0)
- pptr[i]->arg2 = -signExtend16(pptr[i]->arg2);
- deletePcode(i+1);
- nchanges++;
- break;
-
- case oINC :
- (pptr[i]->arg2)++;
- deletePcode(i+1);
- nchanges++;
- break;
-
- case oDEC :
- (pptr[i]->arg2)--;
- deletePcode(i+1);
- nchanges++;
- break;
-
- case oNOT :
- pptr[i]->arg2 = ~(pptr[i]->arg2);
- deletePcode(i+1);
- nchanges++;
- break;
-
- /* Simplify binary operations on constants */
-
- case oADD :
- if (pptr[i]->arg2 == 0)
- {
- deletePcodePair(i, (i+1));
- nchanges++;
- } /* end if */
- else if (pptr[i]->arg2 == 1)
- {
- pptr[i+1]->op = oINC;
- deletePcode(i);
- nchanges++;
- } /* end else if */
- else if (pptr[i]->arg2 == (uint16)-1)
- {
- pptr[i+1]->op = oDEC;
- deletePcode(i);
- nchanges++;
- } /* end else if */
- else i++;
- break;
-
- case oSUB :
- if (pptr[i]->arg2 == 0)
- {
- deletePcodePair(i, (i+1));
- nchanges++;
- } /* end if */
- else if (pptr[i]->arg2 == 1)
- {
- pptr[i+1]->op = oDEC;
- deletePcode(i);
- nchanges++;
- } /* end else if */
- else if (pptr[i]->arg2 == (uint16)-1)
- {
- pptr[i+1]->op = oINC;
- deletePcode(i);
- nchanges++;
- } /* end else if */
- else i++;
- break;
-
- case oMUL :
- case oDIV :
- temp = 0;
- switch (pptr[i]->arg2)
- {
- case 1 :
- deletePcodePair(i, (i+1));
- nchanges++;
- break;
- case 16384 : temp++;
- case 8192 : temp++;
- case 4096 : temp++;
- case 2048 : temp++;
- case 1024 : temp++;
- case 512 : temp++;
- case 256 : temp++;
- case 128 : temp++;
- case 64 : temp++;
- case 32 : temp++;
- case 16 : temp++;
- case 8 : temp++;
- case 4 : temp++;
- case 2 : temp++;
- pptr[i]->arg2 = temp;
- if (pptr[i+1]->op == oMUL)
- pptr[i+1]->op = oSLL;
- else
- pptr[i+1]->op = oSRA;
- nchanges++;
- i++;
- break;
-
- default :
- i++;
- break;
- } /* end switch */
- break;
-
- case oSLL :
- case oSRL :
- case oSRA :
- case oOR :
- if (pptr[i]->arg2 == 0)
- {
- deletePcodePair(i, (i+1));
- nchanges++;
- } /* end if */
- else i++;
- break;
-
- case oAND :
- if (pptr[i]->arg2 == 0xffff)
- {
- deletePcodePair(i, (i+1));
- nchanges++;
- } /* end if */
- else i++;
- break;
-
- /* Delete comparisons of constants to zero */
-
- case oEQUZ :
- if (pptr[i]->arg2 == 0) pptr[i]->arg2 = -1;
- else pptr[i]->arg2 = 0;
- deletePcode(i+1);
- nchanges++;
- break;
-
- case oNEQZ :
- if (pptr[i]->arg2 != 0)
- pptr[i]->arg2 = -1;
- else
- pptr[i]->arg2 = 0;
- deletePcode(i+1);
- nchanges++;
- break;
-
- case oLTZ :
- if (signExtend16(pptr[i]->arg2) < 0)
- pptr[i]->arg2 = -1;
- else
- pptr[i]->arg2 = 0;
- deletePcode(i+1);
- nchanges++;
- break;
-
- case oGTEZ :
- if (signExtend16(pptr[i]->arg2) >= 0)
- pptr[i]->arg2 = -1;
- else
- pptr[i]->arg2 = 0;
- deletePcode(i+1);
- nchanges++;
- break;
-
- case oGTZ :
- if (pptr[i]->arg2 > 0) pptr[i]->arg2 = -1;
- else pptr[i]->arg2 = 0;
- deletePcode(i+1);
- nchanges++;
- break;
-
- case oLTEZ :
- if (pptr[i]->arg2 <= 0) pptr[i]->arg2 = -1;
- else pptr[i]->arg2 = 0;
- deletePcode(i+1);
- nchanges++;
- break;
-
- /* Simplify comparisons with certain constants */
-
- case oEQU :
- if (pptr[i]->arg2 == 0)
- {
- pptr[i+1]->op = oEQUZ;
- deletePcode(i);
- nchanges++;
- } /* end if */
- else if (pptr[i]->arg2 == 1)
- {
- pptr[i]->op = oDEC;
- pptr[i]->arg2 = 0;
- pptr[i+1]->op = oEQUZ;
- nchanges++;
- } /* end else if */
- else if (signExtend16(pptr[i]->arg2) == -1)
- {
- pptr[i]->op = oINC;
- pptr[i]->arg2 = 0;
- pptr[i+1]->op = oEQUZ;
- nchanges++;
- } /* end else if */
- else i++;
- break;
-
- case oNEQ :
- if (pptr[i]->arg2 == 0)
- {
- pptr[i+1]->op = oNEQZ;
- deletePcode(i);
- nchanges++;
- } /* end if */
- else if (pptr[i]->arg2 == 1)
- {
- pptr[i]->op = oDEC;
- pptr[i]->arg2 = 0;
- pptr[i+1]->op = oNEQZ;
- nchanges++;
- } /* end else if */
- else if (signExtend16(pptr[i]->arg2) == -1)
- {
- pptr[i]->op = oINC;
- pptr[i]->arg2 = 0;
- pptr[i+1]->op = oNEQZ;
- nchanges++;
- } /* end else if */
- else i++;
- break;
-
- case oLT :
- if (pptr[i]->arg2 == 0)
- {
- pptr[i+1]->op = oLTZ;
- deletePcode(i);
- nchanges++;
- } /* end if */
- else if (pptr[i]->arg2 == 1)
- {
- pptr[i]->op = oDEC;
- pptr[i]->arg2 = 0;
- pptr[i+1]->op = oLTZ;
- nchanges++;
- } /* end else if */
- else if (signExtend16(pptr[i]->arg2) == -1)
- {
- pptr[i]->op = oINC;
- pptr[i]->arg2 = 0;
- pptr[i+1]->op = oLTZ;
- nchanges++;
- } /* end else if */
- else i++;
- break;
-
- case oGTE :
- if (pptr[i]->arg2 == 0)
- {
- pptr[i+1]->op = oGTEZ;
- deletePcode(i);
- nchanges++;
- } /* end if */
- else if (pptr[i]->arg2 == 1)
- {
- pptr[i]->op = oDEC;
- pptr[i]->arg2 = 0;
- pptr[i+1]->op = oGTEZ;
- nchanges++;
- } /* end else if */
- else if (signExtend16(pptr[i]->arg2) == -1)
- {
- pptr[i]->op = oINC;
- pptr[i]->arg2 = 0;
- pptr[i+1]->op = oGTEZ;
- nchanges++;
- } /* end else if */
- else i++;
- break;
-
- case oGT :
- if (pptr[i]->arg2 == 0)
- {
- pptr[i+1]->op = oGTZ;
- deletePcode(i);
- nchanges++;
- } /* end if */
- else if (pptr[i]->arg2 == 1)
- {
- pptr[i]->op = oDEC;
- pptr[i]->arg2 = 0;
- pptr[i+1]->op = oGTZ;
- nchanges++;
- } /* end else if */
- else if (signExtend16(pptr[i]->arg2) == -1)
- {
- pptr[i]->op = oINC;
- pptr[i]->arg2 = 0;
- pptr[i+1]->op = oGTZ;
- nchanges++;
- } /* end else if */
- else i++;
- break;
-
- case oLTE :
- if (pptr[i]->arg2 == 0)
- {
- pptr[i+1]->op = oLTEZ;
- deletePcode(i);
- nchanges++;
- } /* end if */
- else if (pptr[i]->arg2 == 1)
- {
- pptr[i]->op = oDEC;
- pptr[i]->arg2 = 0;
- pptr[i+1]->op = oLTEZ;
- nchanges++;
- } /* end else if */
- else if (signExtend16(pptr[i]->arg2) == -1)
- {
- pptr[i]->op = oINC;
- pptr[i]->arg2 = 0;
- pptr[i+1]->op = oLTEZ;
- nchanges++;
- } /* end else if */
- else i++;
- break;
-
- /* Simplify or delete condition branches on constants */
-
- case oJEQUZ :
- if (pptr[i]->arg2 == 0)
- {
- pptr[i+1]->op = oJMP;
- deletePcode(i);
- } /* end if */
- else
- deletePcodePair(i, (i+1));
- nchanges++;
- break;
-
- case oJNEQZ :
- if (pptr[i]->arg2 != 0)
- {
- pptr[i+1]->op = oJMP;
- deletePcode(i);
- } /* end if */
- else
- deletePcodePair(i, (i+1));
- nchanges++;
- break;
-
- case oJLTZ :
- if (signExtend16(pptr[i]->arg2) < 0)
- {
- pptr[i+1]->op = oJMP;
- deletePcode(i);
- } /* end if */
- else
- deletePcodePair(i, (i+1));
- nchanges++;
- break;
-
- case oJGTEZ :
- if (signExtend16(pptr[i]->arg2) >= 0)
- {
- pptr[i+1]->op = oJMP;
- deletePcode(i);
- } /* end if */
- else
- deletePcodePair(i, (i+1));
- nchanges++;
- break;
-
- case oJGTZ :
- if (pptr[i]->arg2 > 0)
- {
- pptr[i+1]->op = oJMP;
- deletePcode(i);
- } /* end if */
- else
- deletePcodePair(i, (i+1));
- nchanges++;
- break;
-
- case oJLTEZ :
- if (pptr[i]->arg2 <= 0)
- {
- pptr[i+1]->op = oJMP;
- deletePcode(i);
- } /* end if */
- else
- deletePcodePair(i, (i+1));
- nchanges++;
- break;
-
- default :
- i++;
- break;
- } /* end switch */
-
- /* If the oPUSH instruction is still there, see if we can now */
- /* represent it with an oPUSHB instruction */
-
- if ((pptr[i]->op == oPUSH) && (pptr[i]->arg2 < 256))
- {
- pptr[i]->op = oPUSHB;
- pptr[i]->arg1 = pptr[i]->arg2;
- pptr[i]->arg2 = 0;
- } /* end if */
- } /* end if */
-
- /* Delete multiple modifications of DSEG pointer */
-
- else if (pptr[i]->op == oINDS)
- {
- if (pptr[i+1]->op == oINDS)
- {
- pptr[i]->arg2 += pptr[i+1]->arg2;
- deletePcode(i+1);
- } /* end if */
- else i++;
- } /* end else if */
- else i++;
- } /* end while */
-
- return (nchanges);
-
-} /* end unaryOptimize */
-
-/**********************************************************************/
-
-sint16 binaryOptimize(void)
-{
- sint16 nchanges = 0;
- register sint16 stmp16;
- register sint16 i;
-
- TRACE(stderr, "[binaryOptimize]");
-
- /* At least two pcodes are needed to perform the following binary */
- /* operator optimizations */
-
- i = 0;
- while (i < nops-2)
- {
- if ((pptr[i]->op == oPUSH) || (pptr[i]->op == oPUSHB))
- {
- if ((pptr[i+1]->op == oPUSH) || (pptr[i+1]->op == oPUSHB))
- {
- /* Turn the oPUSHBs into an oPUSHs op (temporarily) */
-
- if (pptr[i]->op == oPUSHB)
- {
- pptr[i]->op = oPUSH;
- pptr[i]->arg2 = pptr[i]->arg1;
- pptr[i]->arg1 = 0;
- } /* end if */
-
- if (pptr[i+1]->op == oPUSHB)
- {
- pptr[i+1]->op = oPUSH;
- pptr[i+1]->arg2 = pptr[i+1]->arg1;
- pptr[i+1]->arg1 = 0;
- } /* end if */
-
- switch (pptr[i+2]->op)
- {
- case oADD :
- pptr[i]->arg2 += pptr[i+1]->arg2;
- deletePcodePair((i+1), (i+2));
- nchanges++;
- break;
-
- case oSUB :
- pptr[i]->arg2 -= pptr[i+1]->arg2;
- deletePcodePair((i+1), (i+2));
- nchanges++;
- break;
-
- case oMUL :
- pptr[i]->arg2 *= pptr[i+1]->arg2;
- deletePcodePair((i+1), (i+2));
- nchanges++;
- break;
-
- case oDIV :
- stmp16 = pptr[i]->arg2 / signExtend16(pptr[i+1]->arg2);
- pptr[i]->arg2 = stmp16;
- deletePcodePair((i+1), (i+2));
- nchanges++;
- break;
-
- case oMOD :
- pptr[i]->arg2 %= pptr[i+1]->arg2;
- deletePcodePair((i+1), (i+2));
- nchanges++;
- break;
-
- case oSLL :
- pptr[i]->arg2 <<= pptr[i+1]->arg2;
- deletePcodePair((i+1), (i+2));
- nchanges++;
- break;
-
- case oSRL :
- pptr[i]->arg2 >>= pptr[i+1]->arg2;
- deletePcodePair((i+1), (i+2));
- nchanges++;
- break;
-
- case oSRA :
- stmp16 = (((sint16)pptr[i]->arg2) >> pptr[i+1]->arg2);
- pptr[i]->arg2 = (uint16)stmp16;
- deletePcodePair((i+1), (i+2));
- nchanges++;
- break;
-
- case oOR :
- pptr[i]->arg2 |= pptr[i+1]->arg2;
- deletePcodePair((i+1), (i+2));
- nchanges++;
- break;
-
- case oAND :
- pptr[i]->arg2 &= pptr[i+1]->arg2;
- deletePcodePair((i+1), (i+2));
- nchanges++;
- break;
-
- case oEQU :
- if (pptr[i]->arg2 == pptr[i+1]->arg2) pptr[i]->arg2 = -1;
- else pptr[i]->arg2 = 0;
- deletePcodePair((i+1), (i+2));
- nchanges++;
- break;
-
- case oNEQ :
- if ((sint16)pptr[i]->arg2 != (sint16)pptr[i+1]->arg2)
- pptr[i]->arg2 = -1;
- else
- pptr[i]->arg2 = 0;
- deletePcodePair((i+1), (i+2));
- nchanges++;
- break;
-
- case oLT :
- if ((sint16)pptr[i]->arg2 < (sint16)pptr[i+1]->arg2)
- pptr[i]->arg2 = -1;
- else
- pptr[i]->arg2 = 0;
- deletePcodePair((i+1), (i+2));
- nchanges++;
- break;
-
- case oGTE :
- if ((sint16)pptr[i]->arg2 >= (sint16)pptr[i+1]->arg2)
- pptr[i]->arg2 = -1;
- else
- pptr[i]->arg2 = 0;
- deletePcodePair((i+1), (i+2));
- nchanges++;
- break;
-
- case oGT :
- if ((sint16)pptr[i]->arg2 > (sint16)pptr[i+1]->arg2)
- pptr[i]->arg2 = -1;
- else
- pptr[i]->arg2 = 0;
- deletePcodePair((i+1), (i+2));
- nchanges++;
- break;
-
- case oLTE :
- if ((sint16)pptr[i]->arg2 <= (sint16)pptr[i+1]->arg2)
- pptr[i]->arg2 = -1;
- else
- pptr[i]->arg2 = 0;
- deletePcodePair((i+1), (i+2));
- nchanges++;
- break;
-
- default :
- i++;
- break;
- } /* end switch */
-
- /* If the oPUSH instruction is still there, see if we can now */
- /* represent it with an oPUSHB instruction */
-
- if (pptr[i] && (pptr[i]->op == oPUSH) && (pptr[i]->arg2 < 256))
- {
- pptr[i]->op = oPUSHB;
- pptr[i]->arg1 = pptr[i]->arg2;
- pptr[i]->arg2 = 0;
- } /* end if */
-
- if (pptr[i+1] && (pptr[i+1]->op == oPUSH) && (pptr[i+1]->arg2 < 256))
- {
- pptr[i+1]->op = oPUSHB;
- pptr[i+1]->arg1 = pptr[i+1]->arg2;
- pptr[i+1]->arg2 = 0;
- } /* end if */
- } /* end if */
-
- /* A single (constant) pcode is sufficient to perform the */
- /* following binary operator optimizations */
-
- else if ((pptr[i+1]->op == oLDSH) || (pptr[i+1]->op == oLDSB) ||
- (pptr[i+1]->op == oLAS) || (pptr[i+1]->op == oLAC))
- {
- /* Turn the oPUSHB into a oPUSH op (temporarily) */
-
- if (pptr[i]->op == oPUSHB)
- {
- pptr[i]->op = oPUSH;
- pptr[i]->arg2 = pptr[i]->arg1;
- pptr[i]->arg1 = 0;
- } /* end if */
-
- switch (pptr[i+2]->op)
- {
- case oADD :
- if (pptr[i]->arg2 == 0)
- {
- deletePcodePair(i, (i+2));
- nchanges++;
- } /* end if */
- else if (pptr[i]->arg2 == 1)
- {
- pptr[i+2]->op = oINC;
- deletePcode(i);
- nchanges++;
- } /* end else if */
- else if (pptr[i]->arg2 == (uint16)-1)
- {
- pptr[i+2]->op = oDEC;
- deletePcode(i);
- nchanges++;
- } /* end else if */
- else i++;
- break;
-
- case oSUB :
- if (pptr[i]->arg2 == 0)
- {
- pptr[i]->op = oNEG;
- deletePcode(i);
- nchanges++;
- } /* end if */
- else i++;
- break;
-
- case oMUL :
- stmp16 = 0;
- switch (pptr[i]->arg2)
- {
- case 1 :
- deletePcodePair(i, (i+2));
- nchanges++;
- break;
- case 16384 : stmp16++;
- case 8192 : stmp16++;
- case 4096 : stmp16++;
- case 2048 : stmp16++;
- case 1024 : stmp16++;
- case 512 : stmp16++;
- case 256 : stmp16++;
- case 128 : stmp16++;
- case 64 : stmp16++;
- case 32 : stmp16++;
- case 16 : stmp16++;
- case 8 : stmp16++;
- case 4 : stmp16++;
- case 2 : stmp16++;
- pptr[i]->op = pptr[i+1]->op;
- pptr[i]->arg1 = pptr[i+1]->arg1;
- pptr[i]->arg2 = pptr[i+1]->arg2;
- pptr[i+1]->op = oPUSH;
- pptr[i+1]->arg1 = 0;
- pptr[i+1]->arg2 = stmp16;
- pptr[i+2]->op = oSLL;
- nchanges++;
- i++;
- break;
-
- default :
- i++;
- break;
- } /* end switch */
- break;
-
- case oOR :
- if (pptr[i]->arg2 == 0)
- {
- deletePcodePair(i, (i+2));
- nchanges++;
- } /* end if */
- else i++;
- break;
-
- case oAND :
- if (pptr[i]->arg2 == 0xffff)
- {
- deletePcodePair(i, (i+2));
- nchanges++;
- } /* end if */
- else i++;
- break;
-
- case oEQU :
- if (pptr[i]->arg2 == 0)
- {
- pptr[i+2]->op = oEQUZ;
- deletePcode(i);
- nchanges++;
- } /* end if */
- else i++;
- break;
-
- case oNEQ :
- if (pptr[i]->arg2 == 0)
- {
- pptr[i+2]->op = oNEQZ;
- deletePcode(i);
- nchanges++;
- } /* end if */
- else i++;
- break;
-
- case oLT :
- if (pptr[i]->arg2 == 0)
- {
- pptr[i+2]->op = oGTEZ;
- deletePcode(i);
- nchanges++;
- } /* end if */
- else i++;
- break;
-
- case oGTE :
- if (pptr[i]->arg2 == 0)
- {
- pptr[i+2]->op = oLTZ;
- deletePcode(i);
- nchanges++;
- } /* end if */
- else i++;
- break;
-
- case oGT :
- if (pptr[i]->arg2 == 0)
- {
- pptr[i+2]->op = oLTEZ;
- deletePcode(i);
- nchanges++;
- } /* end if */
- else i++;
- break;
-
- case oLTE :
- if (pptr[i]->arg2 == 0)
- {
- pptr[i+2]->op = oGTZ;
- deletePcode(i);
- nchanges++;
- } /* end if */
- else i++;
- break;
-
- default :
- i++;
- break;
-
- } /* end switch */
-
- /* If the oPUSH instruction is still there, see if we can now */
- /* represent it with an oPUSHB instruction */
-
- if ((pptr[i]->op == oPUSH) && (pptr[i]->arg2 < 256))
- {
- pptr[i]->op = oPUSHB;
- pptr[i]->arg1 = pptr[i]->arg2;
- pptr[i]->arg2 = 0;
- } /* end if */
- } /* end else if */
- else i++;
- } /* end if */
-
- /* Misc improvements on binary operators */
-
- else if (pptr[i]->op == oNEG)
- {
- /* Negation followed by add is subtraction */
-
- if (pptr[i+1]->op == oADD)
- {
- pptr[i+1]->op = oSUB;
- deletePcode(i);
- nchanges++;
- }
-
- /* Negation followed by subtraction is addition */
-
- else if (pptr[i]->op == oSUB)
- {
- pptr[i+1]->op = oADD;
- deletePcode(i);
- nchanges++;
- }
- else i++;
- }
- else i++;
- } /* end while */
-
- return (nchanges);
-
-} /* end binaryOptimize */
-
-/**********************************************************************/
-
+/********************************************************************** + * pcopt.c + * Constant Expression Optimizations + * + * Copyright (C) 2008-2009 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 <stdint.h> +#include <stdio.h> + +#include "keywords.h" +#include "pdefs.h" +#include "pinsn16.h" + +#include "paslib.h" +#include "popt.h" +#include "polocal.h" +#include "pcopt.h" + +/**********************************************************************/ + +int16_t unaryOptimize(void) +{ + int16_t nchanges = 0; + register uint16_t temp; + register int16_t i; + + TRACE(stderr, "[unaryOptimize]"); + + /* At least two pcodes are need to perform unary optimizations */ + + i = 0; + while (i < nops-1) + { + /* Check for a constant value being pushed onto the stack */ + + if ((pptr[i]->op == oPUSH) || (pptr[i]->op == oPUSHB)) + { + /* Turn the oPUSHB into an oPUSH op (temporarily) */ + + if (pptr[i]->op == oPUSHB) + { + pptr[i]->op = oPUSH; + pptr[i]->arg2 = pptr[i]->arg1; + pptr[i]->arg1 = 0; + } /* end if */ + + switch (pptr[i+1]->op) + { + /* Delete unary operators on constants */ + case oNEG : + pptr[i]->arg2 = -(pptr[i]->arg2); + deletePcode(i+1); + nchanges++; + break; + + case oABS : + if (signExtend16(pptr[i]->arg2) < 0) + pptr[i]->arg2 = -signExtend16(pptr[i]->arg2); + deletePcode(i+1); + nchanges++; + break; + + case oINC : + (pptr[i]->arg2)++; + deletePcode(i+1); + nchanges++; + break; + + case oDEC : + (pptr[i]->arg2)--; + deletePcode(i+1); + nchanges++; + break; + + case oNOT : + pptr[i]->arg2 = ~(pptr[i]->arg2); + deletePcode(i+1); + nchanges++; + break; + + /* Simplify binary operations on constants */ + + case oADD : + if (pptr[i]->arg2 == 0) + { + deletePcodePair(i, (i+1)); + nchanges++; + } /* end if */ + else if (pptr[i]->arg2 == 1) + { + pptr[i+1]->op = oINC; + deletePcode(i); + nchanges++; + } /* end else if */ + else if (pptr[i]->arg2 == (uint16_t)-1) + { + pptr[i+1]->op = oDEC; + deletePcode(i); + nchanges++; + } /* end else if */ + else i++; + break; + + case oSUB : + if (pptr[i]->arg2 == 0) + { + deletePcodePair(i, (i+1)); + nchanges++; + } /* end if */ + else if (pptr[i]->arg2 == 1) + { + pptr[i+1]->op = oDEC; + deletePcode(i); + nchanges++; + } /* end else if */ + else if (pptr[i]->arg2 == (uint16_t)-1) + { + pptr[i+1]->op = oINC; + deletePcode(i); + nchanges++; + } /* end else if */ + else i++; + break; + + case oMUL : + case oDIV : + temp = 0; + switch (pptr[i]->arg2) + { + case 1 : + deletePcodePair(i, (i+1)); + nchanges++; + break; + case 16384 : temp++; + case 8192 : temp++; + case 4096 : temp++; + case 2048 : temp++; + case 1024 : temp++; + case 512 : temp++; + case 256 : temp++; + case 128 : temp++; + case 64 : temp++; + case 32 : temp++; + case 16 : temp++; + case 8 : temp++; + case 4 : temp++; + case 2 : temp++; + pptr[i]->arg2 = temp; + if (pptr[i+1]->op == oMUL) + pptr[i+1]->op = oSLL; + else + pptr[i+1]->op = oSRA; + nchanges++; + i++; + break; + + default : + i++; + break; + } /* end switch */ + break; + + case oSLL : + case oSRL : + case oSRA : + case oOR : + if (pptr[i]->arg2 == 0) + { + deletePcodePair(i, (i+1)); + nchanges++; + } /* end if */ + else i++; + break; + + case oAND : + if (pptr[i]->arg2 == 0xffff) + { + deletePcodePair(i, (i+1)); + nchanges++; + } /* end if */ + else i++; + break; + + /* Delete comparisons of constants to zero */ + + case oEQUZ : + if (pptr[i]->arg2 == 0) pptr[i]->arg2 = -1; + else pptr[i]->arg2 = 0; + deletePcode(i+1); + nchanges++; + break; + + case oNEQZ : + if (pptr[i]->arg2 != 0) + pptr[i]->arg2 = -1; + else + pptr[i]->arg2 = 0; + deletePcode(i+1); + nchanges++; + break; + + case oLTZ : + if (signExtend16(pptr[i]->arg2) < 0) + pptr[i]->arg2 = -1; + else + pptr[i]->arg2 = 0; + deletePcode(i+1); + nchanges++; + break; + + case oGTEZ : + if (signExtend16(pptr[i]->arg2) >= 0) + pptr[i]->arg2 = -1; + else + pptr[i]->arg2 = 0; + deletePcode(i+1); + nchanges++; + break; + + case oGTZ : + if (pptr[i]->arg2 > 0) pptr[i]->arg2 = -1; + else pptr[i]->arg2 = 0; + deletePcode(i+1); + nchanges++; + break; + + case oLTEZ : + if (pptr[i]->arg2 <= 0) pptr[i]->arg2 = -1; + else pptr[i]->arg2 = 0; + deletePcode(i+1); + nchanges++; + break; + + /* Simplify comparisons with certain constants */ + + case oEQU : + if (pptr[i]->arg2 == 0) + { + pptr[i+1]->op = oEQUZ; + deletePcode(i); + nchanges++; + } /* end if */ + else if (pptr[i]->arg2 == 1) + { + pptr[i]->op = oDEC; + pptr[i]->arg2 = 0; + pptr[i+1]->op = oEQUZ; + nchanges++; + } /* end else if */ + else if (signExtend16(pptr[i]->arg2) == -1) + { + pptr[i]->op = oINC; + pptr[i]->arg2 = 0; + pptr[i+1]->op = oEQUZ; + nchanges++; + } /* end else if */ + else i++; + break; + + case oNEQ : + if (pptr[i]->arg2 == 0) + { + pptr[i+1]->op = oNEQZ; + deletePcode(i); + nchanges++; + } /* end if */ + else if (pptr[i]->arg2 == 1) + { + pptr[i]->op = oDEC; + pptr[i]->arg2 = 0; + pptr[i+1]->op = oNEQZ; + nchanges++; + } /* end else if */ + else if (signExtend16(pptr[i]->arg2) == -1) + { + pptr[i]->op = oINC; + pptr[i]->arg2 = 0; + pptr[i+1]->op = oNEQZ; + nchanges++; + } /* end else if */ + else i++; + break; + + case oLT : + if (pptr[i]->arg2 == 0) + { + pptr[i+1]->op = oLTZ; + deletePcode(i); + nchanges++; + } /* end if */ + else if (pptr[i]->arg2 == 1) + { + pptr[i]->op = oDEC; + pptr[i]->arg2 = 0; + pptr[i+1]->op = oLTZ; + nchanges++; + } /* end else if */ + else if (signExtend16(pptr[i]->arg2) == -1) + { + pptr[i]->op = oINC; + pptr[i]->arg2 = 0; + pptr[i+1]->op = oLTZ; + nchanges++; + } /* end else if */ + else i++; + break; + + case oGTE : + if (pptr[i]->arg2 == 0) + { + pptr[i+1]->op = oGTEZ; + deletePcode(i); + nchanges++; + } /* end if */ + else if (pptr[i]->arg2 == 1) + { + pptr[i]->op = oDEC; + pptr[i]->arg2 = 0; + pptr[i+1]->op = oGTEZ; + nchanges++; + } /* end else if */ + else if (signExtend16(pptr[i]->arg2) == -1) + { + pptr[i]->op = oINC; + pptr[i]->arg2 = 0; + pptr[i+1]->op = oGTEZ; + nchanges++; + } /* end else if */ + else i++; + break; + + case oGT : + if (pptr[i]->arg2 == 0) + { + pptr[i+1]->op = oGTZ; + deletePcode(i); + nchanges++; + } /* end if */ + else if (pptr[i]->arg2 == 1) + { + pptr[i]->op = oDEC; + pptr[i]->arg2 = 0; + pptr[i+1]->op = oGTZ; + nchanges++; + } /* end else if */ + else if (signExtend16(pptr[i]->arg2) == -1) + { + pptr[i]->op = oINC; + pptr[i]->arg2 = 0; + pptr[i+1]->op = oGTZ; + nchanges++; + } /* end else if */ + else i++; + break; + + case oLTE : + if (pptr[i]->arg2 == 0) + { + pptr[i+1]->op = oLTEZ; + deletePcode(i); + nchanges++; + } /* end if */ + else if (pptr[i]->arg2 == 1) + { + pptr[i]->op = oDEC; + pptr[i]->arg2 = 0; + pptr[i+1]->op = oLTEZ; + nchanges++; + } /* end else if */ + else if (signExtend16(pptr[i]->arg2) == -1) + { + pptr[i]->op = oINC; + pptr[i]->arg2 = 0; + pptr[i+1]->op = oLTEZ; + nchanges++; + } /* end else if */ + else i++; + break; + + /* Simplify or delete condition branches on constants */ + + case oJEQUZ : + if (pptr[i]->arg2 == 0) + { + pptr[i+1]->op = oJMP; + deletePcode(i); + } /* end if */ + else + deletePcodePair(i, (i+1)); + nchanges++; + break; + + case oJNEQZ : + if (pptr[i]->arg2 != 0) + { + pptr[i+1]->op = oJMP; + deletePcode(i); + } /* end if */ + else + deletePcodePair(i, (i+1)); + nchanges++; + break; + + case oJLTZ : + if (signExtend16(pptr[i]->arg2) < 0) + { + pptr[i+1]->op = oJMP; + deletePcode(i); + } /* end if */ + else + deletePcodePair(i, (i+1)); + nchanges++; + break; + + case oJGTEZ : + if (signExtend16(pptr[i]->arg2) >= 0) + { + pptr[i+1]->op = oJMP; + deletePcode(i); + } /* end if */ + else + deletePcodePair(i, (i+1)); + nchanges++; + break; + + case oJGTZ : + if (pptr[i]->arg2 > 0) + { + pptr[i+1]->op = oJMP; + deletePcode(i); + } /* end if */ + else + deletePcodePair(i, (i+1)); + nchanges++; + break; + + case oJLTEZ : + if (pptr[i]->arg2 <= 0) + { + pptr[i+1]->op = oJMP; + deletePcode(i); + } /* end if */ + else + deletePcodePair(i, (i+1)); + nchanges++; + break; + + default : + i++; + break; + } /* end switch */ + + /* If the oPUSH instruction is still there, see if we can now */ + /* represent it with an oPUSHB instruction */ + + if ((pptr[i]->op == oPUSH) && (pptr[i]->arg2 < 256)) + { + pptr[i]->op = oPUSHB; + pptr[i]->arg1 = pptr[i]->arg2; + pptr[i]->arg2 = 0; + } /* end if */ + } /* end if */ + + /* Delete multiple modifications of DSEG pointer */ + + else if (pptr[i]->op == oINDS) + { + if (pptr[i+1]->op == oINDS) + { + pptr[i]->arg2 += pptr[i+1]->arg2; + deletePcode(i+1); + } /* end if */ + else i++; + } /* end else if */ + else i++; + } /* end while */ + + return (nchanges); + +} /* end unaryOptimize */ + +/**********************************************************************/ + +int16_t binaryOptimize(void) +{ + int16_t nchanges = 0; + register int16_t stmp16; + register int16_t i; + + TRACE(stderr, "[binaryOptimize]"); + + /* At least two pcodes are needed to perform the following binary */ + /* operator optimizations */ + + i = 0; + while (i < nops-2) + { + if ((pptr[i]->op == oPUSH) || (pptr[i]->op == oPUSHB)) + { + if ((pptr[i+1]->op == oPUSH) || (pptr[i+1]->op == oPUSHB)) + { + /* Turn the oPUSHBs into an oPUSHs op (temporarily) */ + + if (pptr[i]->op == oPUSHB) + { + pptr[i]->op = oPUSH; + pptr[i]->arg2 = pptr[i]->arg1; + pptr[i]->arg1 = 0; + } /* end if */ + + if (pptr[i+1]->op == oPUSHB) + { + pptr[i+1]->op = oPUSH; + pptr[i+1]->arg2 = pptr[i+1]->arg1; + pptr[i+1]->arg1 = 0; + } /* end if */ + + switch (pptr[i+2]->op) + { + case oADD : + pptr[i]->arg2 += pptr[i+1]->arg2; + deletePcodePair((i+1), (i+2)); + nchanges++; + break; + + case oSUB : + pptr[i]->arg2 -= pptr[i+1]->arg2; + deletePcodePair((i+1), (i+2)); + nchanges++; + break; + + case oMUL : + pptr[i]->arg2 *= pptr[i+1]->arg2; + deletePcodePair((i+1), (i+2)); + nchanges++; + break; + + case oDIV : + stmp16 = pptr[i]->arg2 / signExtend16(pptr[i+1]->arg2); + pptr[i]->arg2 = stmp16; + deletePcodePair((i+1), (i+2)); + nchanges++; + break; + + case oMOD : + pptr[i]->arg2 %= pptr[i+1]->arg2; + deletePcodePair((i+1), (i+2)); + nchanges++; + break; + + case oSLL : + pptr[i]->arg2 <<= pptr[i+1]->arg2; + deletePcodePair((i+1), (i+2)); + nchanges++; + break; + + case oSRL : + pptr[i]->arg2 >>= pptr[i+1]->arg2; + deletePcodePair((i+1), (i+2)); + nchanges++; + break; + + case oSRA : + stmp16 = (((int16_t)pptr[i]->arg2) >> pptr[i+1]->arg2); + pptr[i]->arg2 = (uint16_t)stmp16; + deletePcodePair((i+1), (i+2)); + nchanges++; + break; + + case oOR : + pptr[i]->arg2 |= pptr[i+1]->arg2; + deletePcodePair((i+1), (i+2)); + nchanges++; + break; + + case oAND : + pptr[i]->arg2 &= pptr[i+1]->arg2; + deletePcodePair((i+1), (i+2)); + nchanges++; + break; + + case oEQU : + if (pptr[i]->arg2 == pptr[i+1]->arg2) pptr[i]->arg2 = -1; + else pptr[i]->arg2 = 0; + deletePcodePair((i+1), (i+2)); + nchanges++; + break; + + case oNEQ : + if ((int16_t)pptr[i]->arg2 != (int16_t)pptr[i+1]->arg2) + pptr[i]->arg2 = -1; + else + pptr[i]->arg2 = 0; + deletePcodePair((i+1), (i+2)); + nchanges++; + break; + + case oLT : + if ((int16_t)pptr[i]->arg2 < (int16_t)pptr[i+1]->arg2) + pptr[i]->arg2 = -1; + else + pptr[i]->arg2 = 0; + deletePcodePair((i+1), (i+2)); + nchanges++; + break; + + case oGTE : + if ((int16_t)pptr[i]->arg2 >= (int16_t)pptr[i+1]->arg2) + pptr[i]->arg2 = -1; + else + pptr[i]->arg2 = 0; + deletePcodePair((i+1), (i+2)); + nchanges++; + break; + + case oGT : + if ((int16_t)pptr[i]->arg2 > (int16_t)pptr[i+1]->arg2) + pptr[i]->arg2 = -1; + else + pptr[i]->arg2 = 0; + deletePcodePair((i+1), (i+2)); + nchanges++; + break; + + case oLTE : + if ((int16_t)pptr[i]->arg2 <= (int16_t)pptr[i+1]->arg2) + pptr[i]->arg2 = -1; + else + pptr[i]->arg2 = 0; + deletePcodePair((i+1), (i+2)); + nchanges++; + break; + + default : + i++; + break; + } /* end switch */ + + /* If the oPUSH instruction is still there, see if we can now */ + /* represent it with an oPUSHB instruction */ + + if (pptr[i] && (pptr[i]->op == oPUSH) && (pptr[i]->arg2 < 256)) + { + pptr[i]->op = oPUSHB; + pptr[i]->arg1 = pptr[i]->arg2; + pptr[i]->arg2 = 0; + } /* end if */ + + if (pptr[i+1] && (pptr[i+1]->op == oPUSH) && (pptr[i+1]->arg2 < 256)) + { + pptr[i+1]->op = oPUSHB; + pptr[i+1]->arg1 = pptr[i+1]->arg2; + pptr[i+1]->arg2 = 0; + } /* end if */ + } /* end if */ + + /* A single (constant) pcode is sufficient to perform the */ + /* following binary operator optimizations */ + + else if ((pptr[i+1]->op == oLDSH) || (pptr[i+1]->op == oLDSB) || + (pptr[i+1]->op == oLAS) || (pptr[i+1]->op == oLAC)) + { + /* Turn the oPUSHB into a oPUSH op (temporarily) */ + + if (pptr[i]->op == oPUSHB) + { + pptr[i]->op = oPUSH; + pptr[i]->arg2 = pptr[i]->arg1; + pptr[i]->arg1 = 0; + } /* end if */ + + switch (pptr[i+2]->op) + { + case oADD : + if (pptr[i]->arg2 == 0) + { + deletePcodePair(i, (i+2)); + nchanges++; + } /* end if */ + else if (pptr[i]->arg2 == 1) + { + pptr[i+2]->op = oINC; + deletePcode(i); + nchanges++; + } /* end else if */ + else if (pptr[i]->arg2 == (uint16_t)-1) + { + pptr[i+2]->op = oDEC; + deletePcode(i); + nchanges++; + } /* end else if */ + else i++; + break; + + case oSUB : + if (pptr[i]->arg2 == 0) + { + pptr[i]->op = oNEG; + deletePcode(i); + nchanges++; + } /* end if */ + else i++; + break; + + case oMUL : + stmp16 = 0; + switch (pptr[i]->arg2) + { + case 1 : + deletePcodePair(i, (i+2)); + nchanges++; + break; + case 16384 : stmp16++; + case 8192 : stmp16++; + case 4096 : stmp16++; + case 2048 : stmp16++; + case 1024 : stmp16++; + case 512 : stmp16++; + case 256 : stmp16++; + case 128 : stmp16++; + case 64 : stmp16++; + case 32 : stmp16++; + case 16 : stmp16++; + case 8 : stmp16++; + case 4 : stmp16++; + case 2 : stmp16++; + pptr[i]->op = pptr[i+1]->op; + pptr[i]->arg1 = pptr[i+1]->arg1; + pptr[i]->arg2 = pptr[i+1]->arg2; + pptr[i+1]->op = oPUSH; + pptr[i+1]->arg1 = 0; + pptr[i+1]->arg2 = stmp16; + pptr[i+2]->op = oSLL; + nchanges++; + i++; + break; + + default : + i++; + break; + } /* end switch */ + break; + + case oOR : + if (pptr[i]->arg2 == 0) + { + deletePcodePair(i, (i+2)); + nchanges++; + } /* end if */ + else i++; + break; + + case oAND : + if (pptr[i]->arg2 == 0xffff) + { + deletePcodePair(i, (i+2)); + nchanges++; + } /* end if */ + else i++; + break; + + case oEQU : + if (pptr[i]->arg2 == 0) + { + pptr[i+2]->op = oEQUZ; + deletePcode(i); + nchanges++; + } /* end if */ + else i++; + break; + + case oNEQ : + if (pptr[i]->arg2 == 0) + { + pptr[i+2]->op = oNEQZ; + deletePcode(i); + nchanges++; + } /* end if */ + else i++; + break; + + case oLT : + if (pptr[i]->arg2 == 0) + { + pptr[i+2]->op = oGTEZ; + deletePcode(i); + nchanges++; + } /* end if */ + else i++; + break; + + case oGTE : + if (pptr[i]->arg2 == 0) + { + pptr[i+2]->op = oLTZ; + deletePcode(i); + nchanges++; + } /* end if */ + else i++; + break; + + case oGT : + if (pptr[i]->arg2 == 0) + { + pptr[i+2]->op = oLTEZ; + deletePcode(i); + nchanges++; + } /* end if */ + else i++; + break; + + case oLTE : + if (pptr[i]->arg2 == 0) + { + pptr[i+2]->op = oGTZ; + deletePcode(i); + nchanges++; + } /* end if */ + else i++; + break; + + default : + i++; + break; + + } /* end switch */ + + /* If the oPUSH instruction is still there, see if we can now */ + /* represent it with an oPUSHB instruction */ + + if ((pptr[i]->op == oPUSH) && (pptr[i]->arg2 < 256)) + { + pptr[i]->op = oPUSHB; + pptr[i]->arg1 = pptr[i]->arg2; + pptr[i]->arg2 = 0; + } /* end if */ + } /* end else if */ + else i++; + } /* end if */ + + /* Misc improvements on binary operators */ + + else if (pptr[i]->op == oNEG) + { + /* Negation followed by add is subtraction */ + + if (pptr[i+1]->op == oADD) + { + pptr[i+1]->op = oSUB; + deletePcode(i); + nchanges++; + } + + /* Negation followed by subtraction is addition */ + + else if (pptr[i]->op == oSUB) + { + pptr[i+1]->op = oADD; + deletePcode(i); + nchanges++; + } + else i++; + } + else i++; + } /* end while */ + + return (nchanges); + +} /* end binaryOptimize */ + +/**********************************************************************/ + diff --git a/misc/pascal/insn16/popt/pcopt.h b/misc/pascal/insn16/popt/pcopt.h index 2a38b481b..02dbae334 100644 --- a/misc/pascal/insn16/popt/pcopt.h +++ b/misc/pascal/insn16/popt/pcopt.h @@ -1,47 +1,53 @@ -/***************************************************************************
- * pcopt.h
- * External Declarations associated with PCOPT.C
- *
- * 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.
- *
- ***************************************************************************/
-
-#ifndef __PCOPT_H
-#define __PCOPT_H
-
-/***************************************************************************
- * Global Function Prototypes
- ***************************************************************************/
-
-extern sint16 unaryOptimize ( void );
-extern sint16 binaryOptimize ( void );
-
-#endif /* __PCOPT_H */
+/*************************************************************************** + * pcopt.h + * External Declarations associated with PCOPT.C + * + * Copyright (C) 200-2009 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. + * + ***************************************************************************/ + +#ifndef __PCOPT_H +#define __PCOPT_H + +/*************************************************************************** + * Included Files + ***************************************************************************/ + +#include <stdint.h> + +/*************************************************************************** + * Global Function Prototypes + ***************************************************************************/ + +extern int16_t unaryOptimize(void); +extern int16_t binaryOptimize(void); + +#endif /* __PCOPT_H */ diff --git a/misc/pascal/insn16/popt/pfopt.c b/misc/pascal/insn16/popt/pfopt.c index 6e74cc90a..0236b525c 100644 --- a/misc/pascal/insn16/popt/pfopt.c +++ b/misc/pascal/insn16/popt/pfopt.c @@ -2,7 +2,7 @@ * pfopt.c * Finalization of optimized image * - * Copyright (C) 2008 Gregory Nutt. All rights reserved. + * Copyright (C) 2008-2009 Gregory Nutt. All rights reserved. * Author: Gregory Nutt <spudmonkey@racsa.co.cr> * * Redistribution and use in source and binary forms, with or without @@ -38,6 +38,7 @@ * Included Files **********************************************************************/ +#include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -84,9 +85,9 @@ static void pass1(poffHandle_t poffHandle, poffProgHandle_t poffProgHandle) { - OPTYPE op; - uint32 pc; - uint32 opsize; + OPTYPE op; + uint32_t pc; + uint32_t opsize; /* Build label / line number reference table * @@ -105,18 +106,18 @@ static void pass1(poffHandle_t poffHandle, poffProgHandle_t poffProgHandle) { opsize = insn_GetOpCode(poffHandle, &op); if (op.op == oLABEL) - { - poffAddToDefinedLabelTable(op.arg2, pc); - } + { + poffAddToDefinedLabelTable(op.arg2, pc); + } else if (op.op == oLINE) - { - poffAddLineNumber(poffHandle, op.arg2, op.arg1, pc); - } + { + poffAddLineNumber(poffHandle, op.arg2, op.arg1, pc); + } else - { - insn_AddTmpOpCode(poffProgHandle, &op); - pc += opsize; - } + { + insn_AddTmpOpCode(poffProgHandle, &op); + pc += opsize; + } } while (op.op != oEND); @@ -130,8 +131,8 @@ static void pass1(poffHandle_t poffHandle, poffProgHandle_t poffProgHandle) static void pass2(poffHandle_t poffHandle, poffProgHandle_t poffProgHandle) { poffSymHandle_t poffSymHandle; - sint32 symIndex; - sint32 nchanges = 0; + int32_t symIndex; + int32_t nchanges = 0; /* Get a container to temporarily hold any modifications that we * make to the symbol table. @@ -153,49 +154,49 @@ static void pass2(poffHandle_t poffHandle, poffProgHandle_t poffProgHandle) poffLibSymbol_t symbol; symIndex = poffGetSymbol(poffHandle, &symbol); if (symIndex >= 0) - { - if ((symbol.type == STT_PROC) || (symbol.type == STT_FUNC)) - { - /* It is a symbol associated with the program data section. - * Has is value been defined? - */ - - if ((symbol.flags & STF_UNDEFINED) != 0) - { - /* No... Add it to the list of undefined labels */ - - poffAddToUndefinedLabelTable(symbol.value, symIndex); - } - else - { - /* It is a defined symbol. In this case, we should have - * encountered its LABEL marker in the pass1 processing - * and the following look up should not fail. - */ - sint32 value = poffGetPcForDefinedLabel(symbol.value); - if (value < 0) - { - DEBUG(stdout, "Failed to find label L%04lx\n", symbol.value); - fatal(ePOFFCONFUSION); - } - else - { - /* Replace the label value with the section offset - * (pc) value. - */ - - symbol.value = value; - nchanges++; - } - } - } - - /* In either event, we will want to save the symbol in case - * we need to re-write the symbol table. - */ - - (void)poffAddTmpSymbol(poffHandle, poffSymHandle, &symbol); - } + { + if ((symbol.type == STT_PROC) || (symbol.type == STT_FUNC)) + { + /* It is a symbol associated with the program data section. + * Has is value been defined? + */ + + if ((symbol.flags & STF_UNDEFINED) != 0) + { + /* No... Add it to the list of undefined labels */ + + poffAddToUndefinedLabelTable(symbol.value, symIndex); + } + else + { + /* It is a defined symbol. In this case, we should have + * encountered its LABEL marker in the pass1 processing + * and the following look up should not fail. + */ + int32_t value = poffGetPcForDefinedLabel(symbol.value); + if (value < 0) + { + DEBUG(stdout, "Failed to find label L%04lx\n", symbol.value); + fatal(ePOFFCONFUSION); + } + else + { + /* Replace the label value with the section offset + * (pc) value. + */ + + symbol.value = value; + nchanges++; + } + } + } + + /* In either event, we will want to save the symbol in case + * we need to re-write the symbol table. + */ + + (void)poffAddTmpSymbol(poffHandle, poffSymHandle, &symbol); + } } while (symIndex >= 0); @@ -218,9 +219,9 @@ static void pass2(poffHandle_t poffHandle, poffProgHandle_t poffProgHandle) static void pass3(poffHandle_t poffHandle, poffProgHandle_t poffProgHandle) { - OPTYPE op; - uint32 pc; - uint32 opsize; + OPTYPE op; + uint32_t pc; + uint32_t opsize; /* Read each opcode, generate relocation information and * replace label references with program section offsets. @@ -243,131 +244,131 @@ static void pass3(poffHandle_t poffHandle, poffProgHandle_t poffProgHandle) { opsize = insn_GetOpCode(poffHandle, &op); switch (op.op) - { - /* Load of an address in the rodata section */ - - case oLAC: - /* We are referencing something from the rodata section. - * No special action need be taken. - */ - break; - - /* Call to a procedure or function. */ - - case oPCAL: - { - /* Check if this is a defined label, i.e., a call to - * procedure or function in the same file. - */ - - sint32 value = poffGetPcForDefinedLabel(op.arg2); - if (value >= 0) - { - /* Yes... replace the label reference with - * a text section offset. No relocation record - * is needed in this case. The only relocation - * may be performed is a subsequent program data - * section offset. - */ - - op.arg2 = (uint16)value; - } - else - { - /* Check if this is a undefined label. This would - * occur for a call to a procedure or a function that - * is defined in some other unit file. - */ - - value = poffGetSymIndexForUndefinedLabel(op.arg2); - if (value >= 0) - { - /* Use the value zero now */ - - op.arg2 = 0; - - /* And generate a symbol-based relocation */ - - (void)poffAddRelocation(poffHandle, RLT_PCAL, value, pc); - } - else - { - DEBUG(stdout, "Failed to find call label L%04x\n", op.arg2); - fatal(ePOFFCONFUSION); - } - } - } - break; - - /* Jumps to "nearby" addresses */ - - case oJMP: /* Unconditional */ - case oJEQUZ: /* Unary comparisons with zero */ - case oJNEQZ: - case oJLTZ: - case oJGTEZ: - case oJGTZ: - case oJLTEZ: - case oJEQU: /* Binary comparisons */ - case oJNEQ: - case oJLT: - case oJGTE: - case oJGT: - case oJLTE: - { - /* Check if this is a defined label. This must be the case - * because there can be no jumps into a unit file. - */ - - sint32 value = poffGetPcForDefinedLabel(op.arg2); - if (value >= 0) - { - /* Yes... replace the label reference with - * a text section offset. No relocation record - * is needed in this case. The only relocation - * may be performed is a subsequent program data - * sectioin offset. - */ - - op.arg2 = (uint16)value; - } - else - { - DEBUG(stdout, "Failed to find jump label L%04x\n", op.arg2); - fatal(ePOFFCONFUSION); - } - } - break; - - /* References to stack via level offset */ - - case oLAS: /* Load stack address */ - case oLASX: - case oLDS: /* Load value */ - case oLDSH: - case oLDSB: - case oLDSM: - case oSTS: /* Store value */ - case oSTSH: - case oSTSB: - case oSTSM: - case oLDSX: - case oLDSXH: /* Load value indexed */ - case oLDSXB: - case oLDSXM: - case oSTSX: /* Store value indexed */ - case oSTSXH: - case oSTSXB: - case oSTSXM: - { + { + /* Load of an address in the rodata section */ + + case oLAC: + /* We are referencing something from the rodata section. + * No special action need be taken. + */ + break; + + /* Call to a procedure or function. */ + + case oPCAL: + { + /* Check if this is a defined label, i.e., a call to + * procedure or function in the same file. + */ + + int32_t value = poffGetPcForDefinedLabel(op.arg2); + if (value >= 0) + { + /* Yes... replace the label reference with + * a text section offset. No relocation record + * is needed in this case. The only relocation + * may be performed is a subsequent program data + * section offset. + */ + + op.arg2 = (uint16_t)value; + } + else + { + /* Check if this is a undefined label. This would + * occur for a call to a procedure or a function that + * is defined in some other unit file. + */ + + value = poffGetSymIndexForUndefinedLabel(op.arg2); + if (value >= 0) + { + /* Use the value zero now */ + + op.arg2 = 0; + + /* And generate a symbol-based relocation */ + + (void)poffAddRelocation(poffHandle, RLT_PCAL, value, pc); + } + else + { + DEBUG(stdout, "Failed to find call label L%04x\n", op.arg2); + fatal(ePOFFCONFUSION); + } + } + } + break; + + /* Jumps to "nearby" addresses */ + + case oJMP: /* Unconditional */ + case oJEQUZ: /* Unary comparisons with zero */ + case oJNEQZ: + case oJLTZ: + case oJGTEZ: + case oJGTZ: + case oJLTEZ: + case oJEQU: /* Binary comparisons */ + case oJNEQ: + case oJLT: + case oJGTE: + case oJGT: + case oJLTE: + { + /* Check if this is a defined label. This must be the case + * because there can be no jumps into a unit file. + */ + + int32_t value = poffGetPcForDefinedLabel(op.arg2); + if (value >= 0) + { + /* Yes... replace the label reference with + * a text section offset. No relocation record + * is needed in this case. The only relocation + * may be performed is a subsequent program data + * sectioin offset. + */ + + op.arg2 = (uint16_t)value; + } + else + { + DEBUG(stdout, "Failed to find jump label L%04x\n", op.arg2); + fatal(ePOFFCONFUSION); + } + } + break; + + /* References to stack via level offset */ + + case oLAS: /* Load stack address */ + case oLASX: + case oLDS: /* Load value */ + case oLDSH: + case oLDSB: + case oLDSM: + case oSTS: /* Store value */ + case oSTSH: + case oSTSB: + case oSTSM: + case oLDSX: + case oLDSXH: /* Load value indexed */ + case oLDSXB: + case oLDSXM: + case oSTSX: /* Store value indexed */ + case oSTSXH: + case oSTSXB: + case oSTSXM: + { #warning REVISIT - } - break; + } + break; - /* Otherwise, it is not an interesting opcode */ - default: - break; - } + /* Otherwise, it is not an interesting opcode */ + default: + break; + } /* Save the potentially modified opcode in the temporary * program data container. @@ -387,9 +388,9 @@ static void pass3(poffHandle_t poffHandle, poffProgHandle_t poffProgHandle) static void pass4(poffHandle_t poffHandle) { - uint32 entryLabel; - sint32 entryOffset; - ubyte fileType; + uint32_t entryLabel; + int32_t entryOffset; + uint8_t fileType; /* What kind of a file did we just process. Was it a program file? * or was it a unit file? @@ -408,9 +409,9 @@ static void pass4(poffHandle_t poffHandle) entryOffset = poffGetPcForDefinedLabel(entryLabel); if (entryOffset < 0) - { - fatal(ePOFFCONFUSION); - } + { + fatal(ePOFFCONFUSION); + } /* Replace file header entry point with the program data * section offset diff --git a/misc/pascal/insn16/popt/pjopt.c b/misc/pascal/insn16/popt/pjopt.c index 4771c72f3..e504a1dce 100644 --- a/misc/pascal/insn16/popt/pjopt.c +++ b/misc/pascal/insn16/popt/pjopt.c @@ -1,449 +1,450 @@ -/**********************************************************************
- * pjopt.c
- * Branch Optimizations
- *
- * 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 "pdefs.h"
-#include "pinsn16.h"
-
-#include "popt.h"
-#include "polocal.h"
-#include "pjopt.h"
-
-/**********************************************************************/
-
-sint16 BranchOptimize (void)
-{
- sint16 nchanges = 0;
- register sint16 i;
-
- TRACE(stderr, "[BranchOptimize]");
-
- /* At least two pcodes are need to perform branch optimizations */
-
- i = 0;
- while (i < nops-1)
- {
- switch (pptr[i]->op)
- {
- case oNOT :
- switch (pptr[i+1]->op)
- {
- case oJEQUZ :
- pptr[i+1]->op = oJNEQZ;
- deletePcode(i);
- nchanges++;
- break;
-
- case oJNEQZ :
- pptr[i+1]->op = oJEQUZ;
- deletePcode(i);
- nchanges++;
- break;
-
- default :
- i++;
- break;
- } /* end switch */
- break;
-
- case oNEG :
- switch (pptr[i+1]->op)
- {
- case oJLTZ :
- pptr[i+1]->op = oJGTZ;
- deletePcode(i);
- nchanges++;
- break;
-
- case oJGTEZ :
- pptr[i+1]->op = oJLTEZ;
- deletePcode(i);
- nchanges++;
- break;
-
- case oJGTZ :
- pptr[i+1]->op = oJLTZ;
- deletePcode(i);
- nchanges++;
- break;
-
- case oJLTEZ :
- pptr[i+1]->op = oJGTEZ;
- deletePcode(i);
- nchanges++;
- break;
-
- default :
- i++;
- break;
- } /* end switch */
- break;
-
- case oEQU :
- switch (pptr[i+1]->op)
- {
- case oNOT :
- pptr[i]->op = oNEQ;
- deletePcode(i+1);
- nchanges++;
- break;
-
- case oJEQUZ :
- pptr[i+1]->op = oJNEQ;
- deletePcode(i);
- nchanges++;
- break;
-
- case oJNEQZ :
- pptr[i+1]->op = oJEQU;
- deletePcode(i);
- nchanges++;
- break;
-
- default :
- i++;
- break;
- } /* end switch */
- break;
-
- case oNEQ :
- switch (pptr[i+1]->op)
- {
- case oNOT :
- pptr[i]->op = oEQU;
- deletePcode(i+1);
- nchanges++;
- break;
-
- case oJEQUZ :
- pptr[i+1]->op = oJEQU;
- deletePcode(i);
- nchanges++;
- break;
-
- case oJNEQZ :
- pptr[i+1]->op = oJNEQ;
- deletePcode(i);
- nchanges++;
- break;
-
- default :
- i++;
- break;
- } /* end switch */
- break;
-
- case oLT :
- switch (pptr[i+1]->op)
- {
- case oNOT :
- pptr[i]->op = oGTE;
- deletePcode(i+1);
- nchanges++;
- break;
-
- case oJEQUZ :
- pptr[i+1]->op = oJGTE;
- deletePcode(i);
- nchanges++;
- break;
-
- case oJNEQZ :
- pptr[i+1]->op = oJLT;
- deletePcode(i);
- nchanges++;
- break;
-
- default :
- i++;
- break;
- } /* end switch */
- break;
-
- case oGTE :
- switch (pptr[i+1]->op)
- {
- case oNOT :
- pptr[i]->op = oLT;
- deletePcode(i+1);
- nchanges++;
- break;
-
- case oJEQUZ :
- pptr[i+1]->op = oJLT;
- deletePcode(i);
- nchanges++;
- break;
-
- case oJNEQZ :
- pptr[i+1]->op = oJGTE;
- deletePcode(i);
- nchanges++;
- break;
-
- default :
- i++;
- break;
- } /* end switch */
- break;
-
- case oGT :
- switch (pptr[i+1]->op)
- {
- case oNOT :
- pptr[i]->op = oLTE;
- deletePcode(i+1);
- nchanges++;
- break;
-
- case oJEQUZ :
- pptr[i+1]->op = oJLTE;
- deletePcode(i);
- nchanges++;
- break;
-
- case oJNEQZ :
- pptr[i+1]->op = oJGT;
- deletePcode(i);
- nchanges++;
- break;
-
- default :
- i++;
- break;
- } /* end switch */
- break;
-
- case oLTE :
- switch (pptr[i+1]->op)
- {
- case oNOT :
- pptr[i]->op = oGT;
- deletePcode(i+1);
- nchanges++;
- break;
-
- case oJEQUZ :
- pptr[i+1]->op = oJGT;
- deletePcode(i);
- nchanges++;
- break;
-
- case oJNEQZ :
- pptr[i+1]->op = oJLTE;
- deletePcode(i);
- nchanges++;
- break;
-
- default :
- i++;
- break;
- } /* end switch */
- break;
-
- case oEQUZ :
- switch (pptr[i+1]->op)
- {
- case oNOT :
- pptr[i]->op = oNEQZ;
- deletePcode(i+1);
- nchanges++;
- break;
-
- case oJEQUZ :
- pptr[i+1]->op = oJNEQZ;
- deletePcode(i);
- nchanges++;
- break;
-
- case oJNEQZ :
- pptr[i+1]->op = oJEQUZ;
- deletePcode(i);
- nchanges++;
- break;
-
- default :
- i++;
- break;
- } /* end switch */
- break;
-
- case oNEQZ :
- switch (pptr[i+1]->op)
- {
- case oNOT :
- pptr[i]->op = oEQUZ;
- deletePcode(i+1);
- nchanges++;
- break;
-
- case oJEQUZ :
- case oJNEQZ :
- deletePcode(i);
- nchanges++;
- break;
-
- default :
- i++;
- break;
- } /* end switch */
- break;
-
- case oLTZ :
- switch (pptr[i+1]->op)
- {
- case oNOT :
- pptr[i]->op = oGTEZ;
- deletePcode(i+1);
- nchanges++;
- break;
-
- case oJEQUZ :
- pptr[i+1]->op = oJGTEZ;
- deletePcode(i);
- nchanges++;
- break;
-
- case oJNEQZ :
- pptr[i+1]->op = oJLTZ;
- deletePcode(i);
- nchanges++;
- break;
-
- default :
- i++;
- break;
- } /* end switch */
- break;
-
- case oGTEZ :
- switch (pptr[i+1]->op)
- {
- case oNOT :
- pptr[i]->op = oLTZ;
- deletePcode(i+1);
- nchanges++;
- break;
-
- case oJEQUZ :
- pptr[i+1]->op = oJLTZ;
- deletePcode(i);
- nchanges++;
- break;
-
- case oJNEQZ :
- pptr[i+1]->op = oJGTEZ;
- deletePcode(i);
- nchanges++;
- break;
-
- default :
- i++;
- break;
- } /* end switch */
- break;
-
- case oGTZ :
- switch (pptr[i+1]->op)
- {
- case oNOT :
- pptr[i]->op = oLTEZ;
- deletePcode(i+1);
- nchanges++;
- break;
-
- case oJEQUZ :
- pptr[i+1]->op = oJLTEZ;
- deletePcode(i);
- nchanges++;
- break;
-
- case oJNEQZ :
- pptr[i+1]->op = oJGTZ;
- deletePcode(i);
- nchanges++;
- break;
-
- default :
- i++;
- break;
- } /* end switch */
- break;
-
- case oLTEZ :
- switch (pptr[i+1]->op)
- {
- case oNOT :
- pptr[i]->op = oGTZ;
- deletePcode(i+1);
- nchanges++;
- break;
-
- case oJEQUZ :
- pptr[i+1]->op = oJGTZ;
- deletePcode(i);
- nchanges++;
- break;
-
- case oJNEQZ :
- pptr[i+1]->op = oJLTEZ;
- deletePcode(i);
- nchanges++;
- break;
-
- default :
- i++;
- break;
- } /* end switch */
- break;
-
- default :
- i++;
- break;
- } /* end switch */
- } /* end while */
- return (nchanges);
-
-} /* end BranchOptimize */
-
-/**********************************************************************/
-
+/********************************************************************** + * pjopt.c + * Branch Optimizations + * + * Copyright (C) 2008-2009 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 <stdint.h> +#include <stdio.h> + +#include "keywords.h" +#include "pdefs.h" +#include "pinsn16.h" + +#include "popt.h" +#include "polocal.h" +#include "pjopt.h" + +/**********************************************************************/ + +int16_t BranchOptimize (void) +{ + int16_t nchanges = 0; + register int16_t i; + + TRACE(stderr, "[BranchOptimize]"); + + /* At least two pcodes are need to perform branch optimizations */ + + i = 0; + while (i < nops-1) + { + switch (pptr[i]->op) + { + case oNOT : + switch (pptr[i+1]->op) + { + case oJEQUZ : + pptr[i+1]->op = oJNEQZ; + deletePcode(i); + nchanges++; + break; + + case oJNEQZ : + pptr[i+1]->op = oJEQUZ; + deletePcode(i); + nchanges++; + break; + + default : + i++; + break; + } /* end switch */ + break; + + case oNEG : + switch (pptr[i+1]->op) + { + case oJLTZ : + pptr[i+1]->op = oJGTZ; + deletePcode(i); + nchanges++; + break; + + case oJGTEZ : + pptr[i+1]->op = oJLTEZ; + deletePcode(i); + nchanges++; + break; + + case oJGTZ : + pptr[i+1]->op = oJLTZ; + deletePcode(i); + nchanges++; + break; + + case oJLTEZ : + pptr[i+1]->op = oJGTEZ; + deletePcode(i); + nchanges++; + break; + + default : + i++; + break; + } /* end switch */ + break; + + case oEQU : + switch (pptr[i+1]->op) + { + case oNOT : + pptr[i]->op = oNEQ; + deletePcode(i+1); + nchanges++; + break; + + case oJEQUZ : + pptr[i+1]->op = oJNEQ; + deletePcode(i); + nchanges++; + break; + + case oJNEQZ : + pptr[i+1]->op = oJEQU; + deletePcode(i); + nchanges++; + break; + + default : + i++; + break; + } /* end switch */ + break; + + case oNEQ : + switch (pptr[i+1]->op) + { + case oNOT : + pptr[i]->op = oEQU; + deletePcode(i+1); + nchanges++; + break; + + case oJEQUZ : + pptr[i+1]->op = oJEQU; + deletePcode(i); + nchanges++; + break; + + case oJNEQZ : + pptr[i+1]->op = oJNEQ; + deletePcode(i); + nchanges++; + break; + + default : + i++; + break; + } /* end switch */ + break; + + case oLT : + switch (pptr[i+1]->op) + { + case oNOT : + pptr[i]->op = oGTE; + deletePcode(i+1); + nchanges++; + break; + + case oJEQUZ : + pptr[i+1]->op = oJGTE; + deletePcode(i); + nchanges++; + break; + + case oJNEQZ : + pptr[i+1]->op = oJLT; + deletePcode(i); + nchanges++; + break; + + default : + i++; + break; + } /* end switch */ + break; + + case oGTE : + switch (pptr[i+1]->op) + { + case oNOT : + pptr[i]->op = oLT; + deletePcode(i+1); + nchanges++; + break; + + case oJEQUZ : + pptr[i+1]->op = oJLT; + deletePcode(i); + nchanges++; + break; + + case oJNEQZ : + pptr[i+1]->op = oJGTE; + deletePcode(i); + nchanges++; + break; + + default : + i++; + break; + } /* end switch */ + break; + + case oGT : + switch (pptr[i+1]->op) + { + case oNOT : + pptr[i]->op = oLTE; + deletePcode(i+1); + nchanges++; + break; + + case oJEQUZ : + pptr[i+1]->op = oJLTE; + deletePcode(i); + nchanges++; + break; + + case oJNEQZ : + pptr[i+1]->op = oJGT; + deletePcode(i); + nchanges++; + break; + + default : + i++; + break; + } /* end switch */ + break; + + case oLTE : + switch (pptr[i+1]->op) + { + case oNOT : + pptr[i]->op = oGT; + deletePcode(i+1); + nchanges++; + break; + + case oJEQUZ : + pptr[i+1]->op = oJGT; + deletePcode(i); + nchanges++; + break; + + case oJNEQZ : + pptr[i+1]->op = oJLTE; + deletePcode(i); + nchanges++; + break; + + default : + i++; + break; + } /* end switch */ + break; + + case oEQUZ : + switch (pptr[i+1]->op) + { + case oNOT : + pptr[i]->op = oNEQZ; + deletePcode(i+1); + nchanges++; + break; + + case oJEQUZ : + pptr[i+1]->op = oJNEQZ; + deletePcode(i); + nchanges++; + break; + + case oJNEQZ : + pptr[i+1]->op = oJEQUZ; + deletePcode(i); + nchanges++; + break; + + default : + i++; + break; + } /* end switch */ + break; + + case oNEQZ : + switch (pptr[i+1]->op) + { + case oNOT : + pptr[i]->op = oEQUZ; + deletePcode(i+1); + nchanges++; + break; + + case oJEQUZ : + case oJNEQZ : + deletePcode(i); + nchanges++; + break; + + default : + i++; + break; + } /* end switch */ + break; + + case oLTZ : + switch (pptr[i+1]->op) + { + case oNOT : + pptr[i]->op = oGTEZ; + deletePcode(i+1); + nchanges++; + break; + + case oJEQUZ : + pptr[i+1]->op = oJGTEZ; + deletePcode(i); + nchanges++; + break; + + case oJNEQZ : + pptr[i+1]->op = oJLTZ; + deletePcode(i); + nchanges++; + break; + + default : + i++; + break; + } /* end switch */ + break; + + case oGTEZ : + switch (pptr[i+1]->op) + { + case oNOT : + pptr[i]->op = oLTZ; + deletePcode(i+1); + nchanges++; + break; + + case oJEQUZ : + pptr[i+1]->op = oJLTZ; + deletePcode(i); + nchanges++; + break; + + case oJNEQZ : + pptr[i+1]->op = oJGTEZ; + deletePcode(i); + nchanges++; + break; + + default : + i++; + break; + } /* end switch */ + break; + + case oGTZ : + switch (pptr[i+1]->op) + { + case oNOT : + pptr[i]->op = oLTEZ; + deletePcode(i+1); + nchanges++; + break; + + case oJEQUZ : + pptr[i+1]->op = oJLTEZ; + deletePcode(i); + nchanges++; + break; + + case oJNEQZ : + pptr[i+1]->op = oJGTZ; + deletePcode(i); + nchanges++; + break; + + default : + i++; + break; + } /* end switch */ + break; + + case oLTEZ : + switch (pptr[i+1]->op) + { + case oNOT : + pptr[i]->op = oGTZ; + deletePcode(i+1); + nchanges++; + break; + + case oJEQUZ : + pptr[i+1]->op = oJGTZ; + deletePcode(i); + nchanges++; + break; + + case oJNEQZ : + pptr[i+1]->op = oJLTEZ; + deletePcode(i); + nchanges++; + break; + + default : + i++; + break; + } /* end switch */ + break; + + default : + i++; + break; + } /* end switch */ + } /* end while */ + return (nchanges); + +} /* end BranchOptimize */ + +/**********************************************************************/ + diff --git a/misc/pascal/insn16/popt/pjopt.h b/misc/pascal/insn16/popt/pjopt.h index ccb2e14b0..4c083e0e3 100644 --- a/misc/pascal/insn16/popt/pjopt.h +++ b/misc/pascal/insn16/popt/pjopt.h @@ -1,42 +1,52 @@ -/***************************************************************************
- * pjopt.h
- * External Declarations associated with pjopt.c
- *
- * 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.
- *
- ***************************************************************************/
-
-#ifndef __PJOPT_H
-#define __PJOPT_H
-
-sint16 BranchOptimize ( void );
-
+/*************************************************************************** + * pjopt.h + * External Declarations associated with pjopt.c + * + * Copyright (C) 2008-2009 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. + * + ***************************************************************************/ + +#ifndef __PJOPT_H +#define __PJOPT_H + +/*************************************************************************** + * Included Files + ***************************************************************************/ + +#include <stdint.h> + +/*************************************************************************** + * Public Function Prototypes + ***************************************************************************/ + +int16_t BranchOptimize(void); + #endif /* __PJOPT_H */
\ No newline at end of file diff --git a/misc/pascal/insn16/popt/plopt.c b/misc/pascal/insn16/popt/plopt.c index 906e35976..2f8ff39f0 100644 --- a/misc/pascal/insn16/popt/plopt.c +++ b/misc/pascal/insn16/popt/plopt.c @@ -1,249 +1,250 @@ -/**********************************************************************
- * plopt.c
- * Load/Store Optimizations
- *
- * 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 "pdefs.h"
-#include "pinsn16.h"
-
-#include "popt.h"
-#include "polocal.h"
-#include "plopt.h"
-
-/**********************************************************************/
-
-sint16 LoadOptimize(void)
-{
- uint16 val;
- sint16 nchanges = 0;
- register sint16 i;
-
- TRACE(stderr, "[LoadOptimize]");
-
- /* At least two pcodes are need to perform Load optimizations */
-
- i = 0;
- while (i < nops-1)
- {
- switch (pptr[i]->op)
- {
- /* Eliminate duplicate loads */
-
- case oLDSH :
- if ((pptr[i+1]->op == oLDSH) &&
- (pptr[i+1]->arg1 == pptr[i]->arg1) &&
- (pptr[i+1]->arg2 == pptr[i]->arg2))
- {
- pptr[i+1]->op = oDUPH;
- pptr[i+1]->arg1 = 0;
- pptr[i+1]->arg2 = 0;
- nchanges++;
- i += 2;
- } /* end if */
- else i++;
- break;
-
- /* Convert loads indexed by a constant to unindexed loads */
-
- case oPUSH :
- case oPUSHB :
- /* Get the index value */
-
- if (pptr[i]->op == oPUSH)
- {
- val = pptr[i]->arg2;
- }
- else
- {
- val = pptr[i]->arg1;
- }
-
- /* If the following instruction is a load, add the constant
- * index value to the address and switch the opcode to the
- * unindexed form.
- */
-
- if (pptr[i+1]->op == oLDSXH)
- {
- pptr[i+1]->op = oLDSH;
- pptr[i+1]->arg2 += val;
- deletePcode (i);
- nchanges++;
- } /* end if */
- else if (pptr[i+1]->op == oLASX)
- {
- pptr[i+1]->op = oLAS;
- pptr[i+1]->arg2 += val;
- deletePcode (i);
- nchanges++;
- } /* end else if */
- else if (pptr[i+1]->op == oLDSXB)
- {
- pptr[i+1]->op = oLDSB;
- pptr[i+1]->arg2 += val;
- deletePcode (i);
- nchanges++;
- } /* end if */
- else if (pptr[i+1]->op == oLDSXM)
- {
- pptr[i+1]->op = oLDSM;
- pptr[i+1]->arg2 += val;
- deletePcode (i);
- nchanges++;
- } /* end if */
- else if (val < 256)
- {
- pptr[i]->op = oPUSHB;
- pptr[i]->arg1 = val;
- pptr[i]->arg2 = 0;
- i++;
- } /* end else if */
- else i++;
- break;
-
- default :
- i++;
- break;
- } /* end switch */
- } /* end while */
- return (nchanges);
-} /* end LoadOptimize */
-
-/**********************************************************************/
-sint16 StoreOptimize (void)
-{
- uint16 val;
- sint16 nchanges = 0;
- register sint16 i;
-
- TRACE(stderr, "[StoreOptimize]");
-
- /* At least two pcodes are need to perform the following Store */
- /* optimizations */
-
- i = 0;
- while (i < nops-1)
- {
- switch (pptr[i]->op)
- {
- /* Eliminate store followed by load */
-
- case oSTSH :
- if ((pptr[i+1]->op == oLDSH) &&
- (pptr[i+1]->arg1 == pptr[i]->arg1) &&
- (pptr[i+1]->arg2 == pptr[i]->arg2))
- {
- pptr[i+1]->op = oSTSH;
- pptr[i]->op = oDUPH;
- pptr[i]->arg1 = 0;
- pptr[i]->arg2 = 0;
- nchanges++;
- i += 2;
- } /* end if */
- else i++;
- break;
-
- /* Convert stores indexed by a constant to unindexed stores */
- case oPUSH :
- /* Get the index value */
-
- if (pptr[i]->op == oPUSH)
- {
- val = pptr[i]->arg2;
- }
- else
- {
- val = pptr[i]->arg1;
- }
-
- /* If the following instruction is a store, add the constant
- * index value to the address and switch the opcode to the
- * unindexed form.
- */
-
- if (i < nops-2)
- {
- if (pptr[i+2]->op == oSTSXH)
- {
- pptr[i+2]->op = oSTSH;
- pptr[i+2]->arg2 += pptr[i]->arg2;
- deletePcode (i);
- nchanges++;
- } /* end if */
- else if (pptr[i+2]->op == oSTSXB)
- {
- pptr[i+2]->op = oSTSB;
- pptr[i+2]->arg2 += pptr[i]->arg2;
- deletePcode (i);
- nchanges++;
- } /* end if */
- else i++;
- } /* end if */
- else i++;
- break;
-
- case oPUSHB :
- if (i < nops-2)
- {
- if (pptr[i+2]->op == oSTSXB)
- {
- pptr[i+2]->op = oSTSB;
- pptr[i+2]->arg2 += pptr[i]->arg2;
- deletePcode (i);
- nchanges++;
- } /* end if */
- else i++;
- } /* end if */
- else i++;
- break;
-
- default :
- i++;
- break;
- } /* end switch */
- } /* end while */
-
- return (nchanges);
-
-} /* end StoreOptimize */
-
-/**********************************************************************/
-
+/********************************************************************** + * plopt.c + * Load/Store Optimizations + * + * Copyright (C) 2008-2009 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 <stdint.h> +#include <stdio.h> + +#include "keywords.h" +#include "pdefs.h" +#include "pinsn16.h" + +#include "popt.h" +#include "polocal.h" +#include "plopt.h" + +/**********************************************************************/ + +int16_t LoadOptimize(void) +{ + uint16_t val; + int16_t nchanges = 0; + register int16_t i; + + TRACE(stderr, "[LoadOptimize]"); + + /* At least two pcodes are need to perform Load optimizations */ + + i = 0; + while (i < nops-1) + { + switch (pptr[i]->op) + { + /* Eliminate duplicate loads */ + + case oLDSH : + if ((pptr[i+1]->op == oLDSH) && + (pptr[i+1]->arg1 == pptr[i]->arg1) && + (pptr[i+1]->arg2 == pptr[i]->arg2)) + { + pptr[i+1]->op = oDUPH; + pptr[i+1]->arg1 = 0; + pptr[i+1]->arg2 = 0; + nchanges++; + i += 2; + } /* end if */ + else i++; + break; + + /* Convert loads indexed by a constant to unindexed loads */ + + case oPUSH : + case oPUSHB : + /* Get the index value */ + + if (pptr[i]->op == oPUSH) + { + val = pptr[i]->arg2; + } + else + { + val = pptr[i]->arg1; + } + + /* If the following instruction is a load, add the constant + * index value to the address and switch the opcode to the + * unindexed form. + */ + + if (pptr[i+1]->op == oLDSXH) + { + pptr[i+1]->op = oLDSH; + pptr[i+1]->arg2 += val; + deletePcode (i); + nchanges++; + } /* end if */ + else if (pptr[i+1]->op == oLASX) + { + pptr[i+1]->op = oLAS; + pptr[i+1]->arg2 += val; + deletePcode (i); + nchanges++; + } /* end else if */ + else if (pptr[i+1]->op == oLDSXB) + { + pptr[i+1]->op = oLDSB; + pptr[i+1]->arg2 += val; + deletePcode (i); + nchanges++; + } /* end if */ + else if (pptr[i+1]->op == oLDSXM) + { + pptr[i+1]->op = oLDSM; + pptr[i+1]->arg2 += val; + deletePcode (i); + nchanges++; + } /* end if */ + else if (val < 256) + { + pptr[i]->op = oPUSHB; + pptr[i]->arg1 = val; + pptr[i]->arg2 = 0; + i++; + } /* end else if */ + else i++; + break; + + default : + i++; + break; + } /* end switch */ + } /* end while */ + return (nchanges); +} /* end LoadOptimize */ + +/**********************************************************************/ +int16_t StoreOptimize (void) +{ + uint16_t val; + int16_t nchanges = 0; + register int16_t i; + + TRACE(stderr, "[StoreOptimize]"); + + /* At least two pcodes are need to perform the following Store */ + /* optimizations */ + + i = 0; + while (i < nops-1) + { + switch (pptr[i]->op) + { + /* Eliminate store followed by load */ + + case oSTSH : + if ((pptr[i+1]->op == oLDSH) && + (pptr[i+1]->arg1 == pptr[i]->arg1) && + (pptr[i+1]->arg2 == pptr[i]->arg2)) + { + pptr[i+1]->op = oSTSH; + pptr[i]->op = oDUPH; + pptr[i]->arg1 = 0; + pptr[i]->arg2 = 0; + nchanges++; + i += 2; + } /* end if */ + else i++; + break; + + /* Convert stores indexed by a constant to unindexed stores */ + case oPUSH : + /* Get the index value */ + + if (pptr[i]->op == oPUSH) + { + val = pptr[i]->arg2; + } + else + { + val = pptr[i]->arg1; + } + + /* If the following instruction is a store, add the constant + * index value to the address and switch the opcode to the + * unindexed form. + */ + + if (i < nops-2) + { + if (pptr[i+2]->op == oSTSXH) + { + pptr[i+2]->op = oSTSH; + pptr[i+2]->arg2 += pptr[i]->arg2; + deletePcode (i); + nchanges++; + } /* end if */ + else if (pptr[i+2]->op == oSTSXB) + { + pptr[i+2]->op = oSTSB; + pptr[i+2]->arg2 += pptr[i]->arg2; + deletePcode (i); + nchanges++; + } /* end if */ + else i++; + } /* end if */ + else i++; + break; + + case oPUSHB : + if (i < nops-2) + { + if (pptr[i+2]->op == oSTSXB) + { + pptr[i+2]->op = oSTSB; + pptr[i+2]->arg2 += pptr[i]->arg2; + deletePcode (i); + nchanges++; + } /* end if */ + else i++; + } /* end if */ + else i++; + break; + + default : + i++; + break; + } /* end switch */ + } /* end while */ + + return (nchanges); + +} /* end StoreOptimize */ + +/**********************************************************************/ + diff --git a/misc/pascal/insn16/popt/plopt.h b/misc/pascal/insn16/popt/plopt.h index b423006a7..eab655930 100644 --- a/misc/pascal/insn16/popt/plopt.h +++ b/misc/pascal/insn16/popt/plopt.h @@ -1,44 +1,54 @@ -/***************************************************************************
- * plopt.h
- * External Declarations associated with plopt.c
- *
- * 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.
- *
- ***************************************************************************/
-
-#ifndef __PLOPT_H
-#define __PLOPT_H
-
-extern sint16 LoadOptimize ( void );
-extern sint16 StoreOptimize ( void );
-
-#endif __PLOPT_H
-
+/*************************************************************************** + * plopt.h + * External Declarations associated with plopt.c + * + * Copyright (C) 2008-2009 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. + * + ***************************************************************************/ + +#ifndef __PLOPT_H +#define __PLOPT_H + +/*************************************************************************** + * Included Files + ***************************************************************************/ + +#include <stdint.h> + +/*************************************************************************** + * Public Function Prototypes + ***************************************************************************/ + +extern int16_t LoadOptimize ( void ); +extern int16_t StoreOptimize ( void ); + +#endif __PLOPT_H + diff --git a/misc/pascal/insn16/popt/polocal.c b/misc/pascal/insn16/popt/polocal.c index c2b2a5b67..39260b1dd 100644 --- a/misc/pascal/insn16/popt/polocal.c +++ b/misc/pascal/insn16/popt/polocal.c @@ -1,300 +1,301 @@ -/**********************************************************************
- * 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();
-}
-
-/***********************************************************************/
+/********************************************************************** + * polocal.c + * P-Code Local Optimizer + * + * Copyright (C) 2008-2009 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 <stdint.h> +#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 */ + +int16_t nops = 0; /* No. Valid Pcode Pointers */ +int16_t 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) +{ + int16_t 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(int16_t delIndex) +{ + TRACE(stderr, "[deletePcode]"); + + pptr[delIndex]->op = oNOP; + pptr[delIndex]->arg1 = 0; + pptr[delIndex]->arg2 = 0; + setupPointer(); +} + +/**********************************************************************/ + +void deletePcodePair(int16_t delIndex1, int16_t 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 int16_t 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 int16_t 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 int16_t 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(); +} + +/***********************************************************************/ diff --git a/misc/pascal/insn16/popt/polocal.h b/misc/pascal/insn16/popt/polocal.h index 4bfb89b24..9e6010dca 100644 --- a/misc/pascal/insn16/popt/polocal.h +++ b/misc/pascal/insn16/popt/polocal.h @@ -1,73 +1,74 @@ -/***************************************************************************
- * polocal.h
- * External Declarations associated with polocal.c
- *
- * 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.
- *
- ***************************************************************************/
-
-#ifndef __POLOCAL_H
-#define __POLOCAL_H
-
-/***************************************************************************
-* Included Files
-****************************************************************************/
-
-#include "keywords.h"
-#include "pdefs.h"
-#include "pofflib.h"
-
-/***************************************************************************
-* Definitions
-****************************************************************************/
-
-#define WINDOW 10 /* size of optimization window */
-
-/***************************************************************************
-* Global Function Prototypes
-****************************************************************************/
-
-extern void localOptimization(poffHandle_t poffHandle,
- poffProgHandle_t poffProgHandle);
-extern void deletePcode (sint16 delIndex);
-extern void deletePcodePair (sint16 delIndex1, sint16 delIndex2);
-
-/***************************************************************************
- * Global Variables
- ****************************************************************************/
-
-extern OPTYPE ptable [WINDOW]; /* Pcode Table */
-extern OPTYPE *pptr [WINDOW]; /* Valid Pcode Pointers */
-
-extern sint16 nops; /* No. Valid Pcode Pointers */
-extern sint16 end_out; /* 1 = oEND pcode has been output */
-
-#endif /* __PLOCAL_H */
+/*************************************************************************** + * polocal.h + * External Declarations associated with polocal.c + * + * Copyright (C) 2008-2009 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. + * + ***************************************************************************/ + +#ifndef __POLOCAL_H +#define __POLOCAL_H + +/*************************************************************************** +* Included Files +****************************************************************************/ + +#include <stdint.h> +#include "keywords.h" +#include "pdefs.h" +#include "pofflib.h" + +/*************************************************************************** +* Definitions +****************************************************************************/ + +#define WINDOW 10 /* size of optimization window */ + +/*************************************************************************** +* Global Function Prototypes +****************************************************************************/ + +extern void localOptimization(poffHandle_t poffHandle, + poffProgHandle_t poffProgHandle); +extern void deletePcode (int16_t delIndex); +extern void deletePcodePair (int16_t delIndex1, int16_t delIndex2); + +/*************************************************************************** + * Global Variables + ****************************************************************************/ + +extern OPTYPE ptable [WINDOW]; /* Pcode Table */ +extern OPTYPE *pptr [WINDOW]; /* Valid Pcode Pointers */ + +extern int16_t nops; /* No. Valid Pcode Pointers */ +extern int16_t end_out; /* 1 = oEND pcode has been output */ + +#endif /* __PLOCAL_H */ diff --git a/misc/pascal/insn16/popt/popt.c b/misc/pascal/insn16/popt/popt.c index 749710ada..9e8e033dc 100644 --- a/misc/pascal/insn16/popt/popt.c +++ b/misc/pascal/insn16/popt/popt.c @@ -1,288 +1,289 @@ -/**********************************************************************
- * popt.c
- * P-Code Optimizer Main Logic
- *
- * 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 "keywords.h"
-#include "podefs.h"
-#include "paslib.h"
-#include "pofflib.h"
-
-#include "pinsn.h"
-#include "popt.h"
-#include "psopt.h"
-#include "polocal.h"
-#include "pfopt.h"
-
-/**********************************************************************
- * Private Function Prototypes
- **********************************************************************/
-
-static void readPoffFile (const char *filename);
-static void pass1 (void);
-static void pass2 (void);
-static void pass3 (void);
-static void writePoffFile (const char *filename);
-
-/**********************************************************************
- * Global Variables
- **********************************************************************/
-
-/**********************************************************************
- * Private Variables
- **********************************************************************/
-
-static poffHandle_t poffHandle; /* Handle to POFF object */
-
-/**********************************************************************
- * Global Functions
- **********************************************************************/
-
-/***********************************************************************/
-
-int main(int argc, char *argv[], char *envp[])
-{
- TRACE(stderr, "[main]");
-
- /* Check for existence of filename argument */
-
- if (argc < 2)
- {
- printf("Filename Required\n");
- exit (1);
- } /* end if */
-
- /* Read the POFF file into memory */
-
- readPoffFile(argv[1]);
-
- /* Performs pass1 optimization */
-
- pass1();
-
- /* Performs pass2 optimization */
-
- insn_ResetOpCodeRead(poffHandle);
- pass2();
-
- /* Create final section offsets and relocation entries */
-
- insn_ResetOpCodeRead(poffHandle);
- pass3();
-
- /* Write the POFF file */
-
- writePoffFile(argv[1]);
- return 0;
-
-} /* End main */
-
-/**********************************************************************
- * Private Functions
- **********************************************************************/
-
-/***********************************************************************/
-
-static void readPoffFile(const char *filename)
-{
- char objname [FNAME_SIZE+1];
- FILE *objFile;
- uint16 errcode;
-
- TRACE(stderr, "[readPoffFile]");
-
- /* Open the pass1 POFF object file -- Use .o1 extension */
-
- (void)extension(filename, "o1", objname, 1);
- if (!(objFile = fopen(objname, "rb")))
- {
- printf("Error Opening %s\n", objname);
- exit(1);
- } /* end if */
-
- /* Get a handle to a POFF input object */
-
- poffHandle = poffCreateHandle();
- if (!poffHandle)
- {
- printf("Could not get POFF handle\n");
- exit(1);
- } /* end if */
-
- /* Read the POFF file into memory */
-
- errcode = poffReadFile(poffHandle, objFile);
- if (errcode != 0)
- {
- printf("Could not read POFF file, errcode=0x%02x\n", errcode);
- exit(1);
- }
-
- /* Close the input file */
-
- fclose(objFile);
-} /* end pass1 */
-
-/***********************************************************************/
-
-static void pass1(void)
-{
- poffProgHandle_t poffProgHandle; /* Handle to temporary POFF object */
-
- TRACE(stderr, "[pass1]");
-
- /* Create a handle to a temporary object to store new POFF program
- * data.
- */
-
- poffProgHandle = poffCreateProgHandle();
- if (!poffProgHandle)
- {
- printf("Could not get POFF handle\n");
- exit(1);
- } /* end if */
-
- /* Clean up garbage left from the wasteful string stack logic */
-
- stringStackOptimize(poffHandle, poffProgHandle);
-
- /* Replace the original program data with the new program data */
-
- poffReplaceProgData(poffHandle, poffProgHandle);
-
- /* Release the temporary POFF object */
-
- poffDestroyProgHandle(poffProgHandle);
-} /* end pass1 */
-
-/***********************************************************************/
-
-static void pass2(void)
-{
- poffProgHandle_t poffProgHandle; /* Handle to temporary POFF object */
-
- TRACE(stderr, "[pass2]");
-
- /* Create a handle to a temporary object to store new POFF program
- * data.
- */
-
- poffProgHandle = poffCreateProgHandle();
- if (!poffProgHandle)
- {
- printf("Could not get POFF handle\n");
- exit(1);
- } /* end if */
-
- /* Perform Local Optimizatin Initialization */
-
- localOptimization(poffHandle, poffProgHandle);
-
- /* Replace the original program data with the new program data */
-
- poffReplaceProgData(poffHandle, poffProgHandle);
-
- /* Release the temporary POFF object */
-
- poffDestroyProgHandle(poffProgHandle);
-} /* end pass2 */
-
-/***********************************************************************/
-
-static void pass3 (void)
-{
- poffProgHandle_t poffProgHandle; /* Handle to temporary POFF object */
- TRACE(stderr, "[pass3]");
-
- /* Create a handle to a temporary object to store new POFF program
- * data.
- */
-
- poffProgHandle = poffCreateProgHandle();
- if (!poffProgHandle)
- {
- printf("Could not get POFF handle\n");
- exit(1);
- } /* end if */
-
- /* Finalize program section, create relocation and line number
- * sections.
- */
-
- optFinalize(poffHandle, poffProgHandle);
-
- /* Release the temporary POFF object */
-
- poffDestroyProgHandle(poffProgHandle);
-}
-
-/***********************************************************************/
-
-static void writePoffFile(const char *filename)
-{
- char optname [FNAME_SIZE+1];
- FILE *optFile;
-
- TRACE(stderr, "[writePoffFile]");
-
- /* Open optimized p-code file -- Use .o extension */
-
- (void)extension(filename, "o", optname, 1);
- if (!(optFile = fopen(optname, "wb")))
- {
- printf("Error Opening %s\n", optname);
- exit(1);
- } /* end if */
-
- /* Then write the new POFF file */
-
- poffWriteFile(poffHandle, optFile);
-
- /* Destroy the POFF object */
-
- poffDestroyHandle(poffHandle);
-
- /* Close the files used on writePoffFile */
-
- (void)fclose(optFile);
-} /* end writePoffFile */
-
-/***********************************************************************/
+/********************************************************************** + * popt.c + * P-Code Optimizer Main Logic + * + * Copyright (C) 2008-2009 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 <stdint.h> +#include <stdio.h> +#include <stdlib.h> + +#include "keywords.h" +#include "podefs.h" +#include "paslib.h" +#include "pofflib.h" + +#include "pinsn.h" +#include "popt.h" +#include "psopt.h" +#include "polocal.h" +#include "pfopt.h" + +/********************************************************************** + * Private Function Prototypes + **********************************************************************/ + +static void readPoffFile (const char *filename); +static void pass1 (void); +static void pass2 (void); +static void pass3 (void); +static void writePoffFile (const char *filename); + +/********************************************************************** + * Global Variables + **********************************************************************/ + +/********************************************************************** + * Private Variables + **********************************************************************/ + +static poffHandle_t poffHandle; /* Handle to POFF object */ + +/********************************************************************** + * Global Functions + **********************************************************************/ + +/***********************************************************************/ + +int main(int argc, char *argv[], char *envp[]) +{ + TRACE(stderr, "[main]"); + + /* Check for existence of filename argument */ + + if (argc < 2) + { + printf("Filename Required\n"); + exit (1); + } /* end if */ + + /* Read the POFF file into memory */ + + readPoffFile(argv[1]); + + /* Performs pass1 optimization */ + + pass1(); + + /* Performs pass2 optimization */ + + insn_ResetOpCodeRead(poffHandle); + pass2(); + + /* Create final section offsets and relocation entries */ + + insn_ResetOpCodeRead(poffHandle); + pass3(); + + /* Write the POFF file */ + + writePoffFile(argv[1]); + return 0; + +} /* End main */ + +/********************************************************************** + * Private Functions + **********************************************************************/ + +/***********************************************************************/ + +static void readPoffFile(const char *filename) +{ + char objname [FNAME_SIZE+1]; + FILE *objFile; + uint16_t errcode; + + TRACE(stderr, "[readPoffFile]"); + + /* Open the pass1 POFF object file -- Use .o1 extension */ + + (void)extension(filename, "o1", objname, 1); + if (!(objFile = fopen(objname, "rb"))) + { + printf("Error Opening %s\n", objname); + exit(1); + } /* end if */ + + /* Get a handle to a POFF input object */ + + poffHandle = poffCreateHandle(); + if (!poffHandle) + { + printf("Could not get POFF handle\n"); + exit(1); + } /* end if */ + + /* Read the POFF file into memory */ + + errcode = poffReadFile(poffHandle, objFile); + if (errcode != 0) + { + printf("Could not read POFF file, errcode=0x%02x\n", errcode); + exit(1); + } + + /* Close the input file */ + + fclose(objFile); +} /* end pass1 */ + +/***********************************************************************/ + +static void pass1(void) +{ + poffProgHandle_t poffProgHandle; /* Handle to temporary POFF object */ + + TRACE(stderr, "[pass1]"); + + /* Create a handle to a temporary object to store new POFF program + * data. + */ + + poffProgHandle = poffCreateProgHandle(); + if (!poffProgHandle) + { + printf("Could not get POFF handle\n"); + exit(1); + } /* end if */ + + /* Clean up garbage left from the wasteful string stack logic */ + + stringStackOptimize(poffHandle, poffProgHandle); + + /* Replace the original program data with the new program data */ + + poffReplaceProgData(poffHandle, poffProgHandle); + + /* Release the temporary POFF object */ + + poffDestroyProgHandle(poffProgHandle); +} /* end pass1 */ + +/***********************************************************************/ + +static void pass2(void) +{ + poffProgHandle_t poffProgHandle; /* Handle to temporary POFF object */ + + TRACE(stderr, "[pass2]"); + + /* Create a handle to a temporary object to store new POFF program + * data. + */ + + poffProgHandle = poffCreateProgHandle(); + if (!poffProgHandle) + { + printf("Could not get POFF handle\n"); + exit(1); + } /* end if */ + + /* Perform Local Optimizatin Initialization */ + + localOptimization(poffHandle, poffProgHandle); + + /* Replace the original program data with the new program data */ + + poffReplaceProgData(poffHandle, poffProgHandle); + + /* Release the temporary POFF object */ + + poffDestroyProgHandle(poffProgHandle); +} /* end pass2 */ + +/***********************************************************************/ + +static void pass3 (void) +{ + poffProgHandle_t poffProgHandle; /* Handle to temporary POFF object */ + TRACE(stderr, "[pass3]"); + + /* Create a handle to a temporary object to store new POFF program + * data. + */ + + poffProgHandle = poffCreateProgHandle(); + if (!poffProgHandle) + { + printf("Could not get POFF handle\n"); + exit(1); + } /* end if */ + + /* Finalize program section, create relocation and line number + * sections. + */ + + optFinalize(poffHandle, poffProgHandle); + + /* Release the temporary POFF object */ + + poffDestroyProgHandle(poffProgHandle); +} + +/***********************************************************************/ + +static void writePoffFile(const char *filename) +{ + char optname [FNAME_SIZE+1]; + FILE *optFile; + + TRACE(stderr, "[writePoffFile]"); + + /* Open optimized p-code file -- Use .o extension */ + + (void)extension(filename, "o", optname, 1); + if (!(optFile = fopen(optname, "wb"))) + { + printf("Error Opening %s\n", optname); + exit(1); + } /* end if */ + + /* Then write the new POFF file */ + + poffWriteFile(poffHandle, optFile); + + /* Destroy the POFF object */ + + poffDestroyHandle(poffHandle); + + /* Close the files used on writePoffFile */ + + (void)fclose(optFile); +} /* end writePoffFile */ + +/***********************************************************************/ diff --git a/misc/pascal/insn16/popt/psopt.c b/misc/pascal/insn16/popt/psopt.c index 8d36ac0aa..0881204c3 100644 --- a/misc/pascal/insn16/popt/psopt.c +++ b/misc/pascal/insn16/popt/psopt.c @@ -2,7 +2,7 @@ * psopt.c * String Stack Optimizaitons * - * Copyright (C) 2008 Gregory Nutt. All rights reserved. + * Copyright (C) 2008-2009 Gregory Nutt. All rights reserved. * Author: Gregory Nutt <spudmonkey@racsa.co.cr> * * Redistribution and use in source and binary forms, with or without @@ -46,6 +46,7 @@ * Included Files **********************************************************************/ +#include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -70,10 +71,10 @@ * Private Data **********************************************************************/ -static ubyte *pbuffer[NPBUFFERS]; -static int nbytes_in_pbuffer[NPBUFFERS]; -static int current_level = -1; -static int inch; +static uint8_t *pbuffer[NPBUFFERS]; +static int nbytes_in_pbuffer[NPBUFFERS]; +static int current_level = -1; +static int inch; /********************************************************************** * Private Function Prototypes @@ -97,7 +98,7 @@ static inline void putbuf(int c, poffProgHandle_t poffProgHandle) { /* No PUSHS encountered. Write byte directly to output */ - poffAddTmpProgByte(poffProgHandle, (ubyte)c); + poffAddTmpProgByte(poffProgHandle, (uint8_t)c); } else { @@ -106,7 +107,7 @@ static inline void putbuf(int c, poffProgHandle_t poffProgHandle) */ int idx = nbytes_in_pbuffer[dlvl]; - ubyte *dest = pbuffer[dlvl] + idx; + uint8_t *dest = pbuffer[dlvl] + idx; *dest = c; nbytes_in_pbuffer[dlvl] = idx + 1; } @@ -122,7 +123,7 @@ static inline void flushc(int c, poffProgHandle_t poffProgHandle) int dlvl = current_level - 1; int idx = nbytes_in_pbuffer[dlvl]; - ubyte *dest = pbuffer[dlvl] + idx; + uint8_t *dest = pbuffer[dlvl] + idx; *dest = c; nbytes_in_pbuffer[dlvl] = idx + 1; } @@ -132,45 +133,45 @@ static inline void flushc(int c, poffProgHandle_t poffProgHandle) * buffer */ - poffAddTmpProgByte(poffProgHandle, (ubyte)c); + poffAddTmpProgByte(poffProgHandle, (uint8_t)c); } } static inline void flushbuf(poffProgHandle_t poffProgHandle) { - uint16 errCode; + uint16_t errCode; int slvl = current_level; if (nbytes_in_pbuffer[slvl] > 0) { if (current_level > 0) - { - /* Nested PUSHS encountered. Flush buffer into buffer associated - * with the previous nesting level. - */ - - int dlvl = slvl - 1; - ubyte *src = pbuffer[slvl]; - ubyte *dest = pbuffer[dlvl] + nbytes_in_pbuffer[dlvl]; - - memcpy(dest, src, nbytes_in_pbuffer[slvl]); - nbytes_in_pbuffer[dlvl] += nbytes_in_pbuffer[slvl]; - } + { + /* Nested PUSHS encountered. Flush buffer into buffer associated + * with the previous nesting level. + */ + + int dlvl = slvl - 1; + uint8_t *src = pbuffer[slvl]; + uint8_t *dest = pbuffer[dlvl] + nbytes_in_pbuffer[dlvl]; + + memcpy(dest, src, nbytes_in_pbuffer[slvl]); + nbytes_in_pbuffer[dlvl] += nbytes_in_pbuffer[slvl]; + } else - { - /* Only one PUSHS encountered. Flush directly to the output - * buffer - */ - - errCode = poffWriteTmpProgBytes(pbuffer[0], nbytes_in_pbuffer[0], - poffProgHandle); - - if (errCode != eNOERROR) - { - printf("Error writing to file: %d\n", errCode); - exit(1); - } - } + { + /* Only one PUSHS encountered. Flush directly to the output + * buffer + */ + + errCode = poffWriteTmpProgBytes(pbuffer[0], nbytes_in_pbuffer[0], + poffProgHandle); + + if (errCode != eNOERROR) + { + printf("Error writing to file: %d\n", errCode); + exit(1); + } + } } nbytes_in_pbuffer[slvl] = 0; } @@ -188,49 +189,49 @@ static void dopush(poffHandle_t poffHandle, poffProgHandle_t poffProgHandle) /* Search for a PUSHS opcode */ if (inch != oPUSHS) - { - /* Its not PUSHS, just echo to the output file/buffer */ + { + /* Its not PUSHS, just echo to the output file/buffer */ - putbuf(inch, poffProgHandle); + putbuf(inch, poffProgHandle); - /* Get the next byte from the input stream */ + /* Get the next byte from the input stream */ - opcode = inch; - inch = poffGetProgByte(poffHandle); + opcode = inch; + inch = poffGetProgByte(poffHandle); - /* Check for an 8-bit argument */ + /* Check for an 8-bit argument */ - if ((opcode & o8) != 0) - { - /* Echo the 8-bit argument */ + if ((opcode & o8) != 0) + { + /* Echo the 8-bit argument */ - putbuf(inch, poffProgHandle); - inch = poffGetProgByte(poffHandle); - } + putbuf(inch, poffProgHandle); + inch = poffGetProgByte(poffHandle); + } - /* Check for a 16-bit argument */ + /* Check for a 16-bit argument */ - if ((opcode & o16) != 0) - { - /* Echo the 16-bit argument */ + if ((opcode & o16) != 0) + { + /* Echo the 16-bit argument */ - putbuf(inch, poffProgHandle); - inch = poffGetProgByte(poffHandle); - putbuf(inch, poffProgHandle); - inch = poffGetProgByte(poffHandle); - } - } + putbuf(inch, poffProgHandle); + inch = poffGetProgByte(poffHandle); + putbuf(inch, poffProgHandle); + inch = poffGetProgByte(poffHandle); + } + } else - { - /* We have found PUSHS. No search for the next occurrence - * of either and instruction that increments the string - * stack or for the matching POPS - */ - - current_level++; - dopop(poffHandle, poffProgHandle); - current_level--; - } + { + /* We have found PUSHS. No search for the next occurrence + * of either and instruction that increments the string + * stack or for the matching POPS + */ + + current_level++; + dopop(poffHandle, poffProgHandle); + current_level--; + } } } @@ -255,88 +256,88 @@ static void dopop(poffHandle_t poffHandle, poffProgHandle_t poffProgHandle) /* Did we encounter another PUSHS? */ if (inch == oPUSHS) - { - /* Yes... recurse to handle it */ + { + /* Yes... recurse to handle it */ - current_level++; - dopop(poffHandle, poffProgHandle); - current_level--; - } + current_level++; + dopop(poffHandle, poffProgHandle); + current_level--; + } else if (inch == oPOPS) - { - /* Flush the buffered data without the PUSHS */ + { + /* Flush the buffered data without the PUSHS */ - flushbuf(poffProgHandle); + flushbuf(poffProgHandle); - /* And discard the matching POPS */ + /* And discard the matching POPS */ - inch = poffGetProgByte(poffHandle); - break; - } + inch = poffGetProgByte(poffHandle); + break; + } else if (inch == oLIB) - { - /* Get the 16-bit argument */ - - putbuf(inch, poffProgHandle); - arg16a = poffGetProgByte(poffHandle); - putbuf(arg16a, poffProgHandle); - arg16b = poffGetProgByte(poffHandle); - putbuf(arg16b, poffProgHandle); - arg16 = (arg16a << 8) | arg16b; - inch = poffGetProgByte(poffHandle); - - /* Is it LIB MKSTK? MKSTKSTR? or MKSTKC? */ - - if ((arg16 == lbMKSTK) || - (arg16 == lbMKSTKSTR) || - (arg16 == lbMKSTKC)) - { - /* Flush the buffered data with the PUSHS */ - - flushc(oPUSHS, poffProgHandle); - flushbuf(poffProgHandle); - - /* And break out of the loop to search for - * the next PUSHS - */ - - break; - } - } + { + /* Get the 16-bit argument */ + + putbuf(inch, poffProgHandle); + arg16a = poffGetProgByte(poffHandle); + putbuf(arg16a, poffProgHandle); + arg16b = poffGetProgByte(poffHandle); + putbuf(arg16b, poffProgHandle); + arg16 = (arg16a << 8) | arg16b; + inch = poffGetProgByte(poffHandle); + + /* Is it LIB MKSTK? MKSTKSTR? or MKSTKC? */ + + if ((arg16 == lbMKSTK) || + (arg16 == lbMKSTKSTR) || + (arg16 == lbMKSTKC)) + { + /* Flush the buffered data with the PUSHS */ + + flushc(oPUSHS, poffProgHandle); + flushbuf(poffProgHandle); + + /* And break out of the loop to search for + * the next PUSHS + */ + + break; + } + } else - { - /* Something else. Put it in the buffer */ + { + /* Something else. Put it in the buffer */ - putbuf(inch, poffProgHandle); + putbuf(inch, poffProgHandle); - /* Get the next byte from the input stream */ + /* Get the next byte from the input stream */ - opcode = inch; - inch = poffGetProgByte(poffHandle); + opcode = inch; + inch = poffGetProgByte(poffHandle); - /* Check for an 8-bit argument */ + /* Check for an 8-bit argument */ - if ((opcode & o8) != 0) - { - /* Buffer the 8-bit argument */ + if ((opcode & o8) != 0) + { + /* Buffer the 8-bit argument */ - putbuf(inch, poffProgHandle); - inch = poffGetProgByte(poffHandle); - } + putbuf(inch, poffProgHandle); + inch = poffGetProgByte(poffHandle); + } - /* Check for a 16-bit argument */ + /* Check for a 16-bit argument */ - if ((opcode & o16) != 0) - { - /* Buffer the 16-bit argument */ + if ((opcode & o16) != 0) + { + /* Buffer the 16-bit argument */ - putbuf(inch, poffProgHandle); - inch = poffGetProgByte(poffHandle); - putbuf(inch, poffProgHandle); - inch = poffGetProgByte(poffHandle); - } - } + putbuf(inch, poffProgHandle); + inch = poffGetProgByte(poffHandle); + putbuf(inch, poffProgHandle); + inch = poffGetProgByte(poffHandle); + } + } } } @@ -345,7 +346,7 @@ static void dopop(poffHandle_t poffHandle, poffProgHandle_t poffProgHandle) **********************************************************************/ void stringStackOptimize(poffHandle_t poffHandle, - poffProgHandle_t poffProgHandle) + poffProgHandle_t poffProgHandle) { int i; @@ -353,12 +354,12 @@ void stringStackOptimize(poffHandle_t poffHandle, for (i = 0; i < NPBUFFERS; i++) { - pbuffer[i] = (ubyte*)malloc(PBUFFER_SIZE); + pbuffer[i] = (uint8_t*)malloc(PBUFFER_SIZE); if (pbuffer[i] == NULL) - { - printf("Failed to allocate pcode buffer\n"); - exit(1); - } + { + printf("Failed to allocate pcode buffer\n"); + exit(1); + } nbytes_in_pbuffer[i] = 0; } |