diff options
author | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2009-12-18 16:30:25 +0000 |
---|---|---|
committer | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2009-12-18 16:30:25 +0000 |
commit | 9695e5c9051d331278288a071c1353ae7214c4c2 (patch) | |
tree | 060301cfc350c1532655c699ab2419e1c4c2e486 /misc/pascal/insn32/popt | |
parent | eb3074a4fe3b7c86d2a3ca139eb881964f2f23e1 (diff) | |
download | px4-nuttx-9695e5c9051d331278288a071c1353ae7214c4c2.tar.gz px4-nuttx-9695e5c9051d331278288a071c1353ae7214c4c2.tar.bz2 px4-nuttx-9695e5c9051d331278288a071c1353ae7214c4c2.zip |
Update to use stdint/stdbool.h
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@2384 42af7a65-404d-4744-a932-0658087f49c3
Diffstat (limited to 'misc/pascal/insn32/popt')
-rw-r--r-- | misc/pascal/insn32/popt/pcopt.c | 1679 | ||||
-rw-r--r-- | misc/pascal/insn32/popt/pfopt.c | 417 | ||||
-rw-r--r-- | misc/pascal/insn32/popt/plopt.c | 429 | ||||
-rw-r--r-- | misc/pascal/insn32/popt/psopt.c | 273 |
4 files changed, 1401 insertions, 1397 deletions
diff --git a/misc/pascal/insn32/popt/pcopt.c b/misc/pascal/insn32/popt/pcopt.c index ee5f4d924..2c4c1fa01 100644 --- a/misc/pascal/insn32/popt/pcopt.c +++ b/misc/pascal/insn32/popt/pcopt.c @@ -1,839 +1,840 @@ -/**********************************************************************
- * 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 "pinsn32.h"
-
-#include "paslib.h"
-#include "popt.h"
-#include "polocal.h"
-#include "pcopt.h"
-
-/**********************************************************************/
-
-int unaryOptimize(void)
-{
- register uint32 temp;
- register int i;
- int nchanges = 0;
-
- 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 (GETOP(pptr[i]) == oPUSH)
- {
- switch (GETOP(pptr[i+1]))
- {
- /* Delete unary operators on constants */
- case oNEG :
- PUTARG(pptr[i], -GETARG(pptr[i]));
- deletePcode(i+1);
- nchanges++;
- break;
-
- case oABS :
- if ((sint32)GETARG(pptr[i]) < 0)
- PUTARG(pptr[i], -(sint32)GETARG(pptr[i]));
- deletePcode(i+1);
- nchanges++;
- break;
-
- case oINC :
- PUTARG(pptr[i], GETARG(pptr[i]) + 1);
- deletePcode(i+1);
- nchanges++;
- break;
-
- case oDEC :
- PUTARG(pptr[i], GETARG(pptr[i]) - 1);
- deletePcode(i+1);
- nchanges++;
- break;
-
- case oNOT :
- PUTARG(pptr[i], ~GETARG(pptr[i]));
- PUTARG(pptr[i], ~(GETARG(pptr[i])));
- deletePcode(i+1);
- nchanges++;
- break;
-
- /* Simplify binary operations on constants */
-
- case oADD :
- if (GETARG(pptr[i]) == 0)
- {
- deletePcodePair(i, (i+1));
- nchanges++;
- } /* end if */
- else if (GETARG(pptr[i]) == 1)
- {
- PUTOP(pptr[i+1], oINC);
- deletePcode(i);
- nchanges++;
- } /* end else if */
- else if (GETARG(pptr[i]) == ARGONES)
- {
- PUTOP(pptr[i+1], oDEC);
- deletePcode(i);
- nchanges++;
- } /* end else if */
- else i++;
- break;
-
- case oSUB :
- if (GETARG(pptr[i]) == 0)
- {
- deletePcodePair(i, (i+1));
- nchanges++;
- } /* end if */
- else if (GETARG(pptr[i]) == 1)
- {
- PUTOP(pptr[i+1], oDEC);
- deletePcode(i);
- nchanges++;
- } /* end else if */
- else if (GETARG(pptr[i]) == ARGONES)
- {
- PUTOP(pptr[i+1], oINC);
- deletePcode(i);
- nchanges++;
- } /* end else if */
- else i++;
- break;
-
- case oMUL :
- case oDIV :
- temp = 0;
- switch (GETARG(pptr[i]))
- {
- 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++;
- PUTARG(pptr[i], temp);
- if (GETOP(pptr[i+1]) == oMUL)
- PUTOP(pptr[i+1], oSLL);
- else
- PUTOP(pptr[i+1], oSRA);
- nchanges++;
- i++;
- break;
-
- default :
- i++;
- break;
- } /* end switch */
- break;
-
- case oSLL :
- case oSRL :
- case oSRA :
- case oOR :
- if (GETARG(pptr[i]) == 0)
- {
- deletePcodePair(i, (i+1));
- nchanges++;
- } /* end if */
- else i++;
- break;
-
- case oAND :
- if (GETARG(pptr[i]) == ARGONES)
- {
- deletePcodePair(i, (i+1));
- nchanges++;
- } /* end if */
- else i++;
- break;
-
- /* Delete comparisons of constants to zero */
-
- case oEQUZ :
- if (GETARG(pptr[i]) == 0)
- PUTARG(pptr[i], -1);
- else
- PUTARG(pptr[i], 0);
- deletePcode(i+1);
- nchanges++;
- break;
-
- case oNEQZ :
- if (GETARG(pptr[i]) != 0)
- PUTARG(pptr[i], -1);
- else
- PUTARG(pptr[i], 0);
- deletePcode(i+1);
- nchanges++;
- break;
-
- case oLTZ :
- if ((sint32)GETARG(pptr[i]) < 0)
- PUTARG(pptr[i], -1);
- else
- PUTARG(pptr[i], 0);
- deletePcode(i+1);
- nchanges++;
- break;
-
- case oGTEZ :
- if ((sint32)GETARG(pptr[i]) >= 0)
- PUTARG(pptr[i], -1);
- else
- PUTARG(pptr[i], 0);
- deletePcode(i+1);
- nchanges++;
- break;
-
- case oGTZ :
- if (GETARG(pptr[i]) > 0)
- PUTARG(pptr[i], -1);
- else
- PUTARG(pptr[i], 0);
- deletePcode(i+1);
- nchanges++;
- break;
-
- case oLTEZ :
- if (GETARG(pptr[i]) <= 0)
- PUTARG(pptr[i], -1);
- else
- PUTARG(pptr[i], 0);
- deletePcode(i+1);
- nchanges++;
- break;
-
- /* Simplify comparisons with certain constants */
-
- case oEQU :
- if (GETARG(pptr[i]) == 0)
- {
- PUTOP(pptr[i+1],oEQUZ);
- deletePcode(i);
- nchanges++;
- } /* end if */
- else if (GETARG(pptr[i]) == 1)
- {
- PUTOP(pptr[i], oDEC);
- PUTARG(pptr[i], 0);
- PUTOP(pptr[i+1], oEQUZ);
- nchanges++;
- } /* end else if */
- else if ((sint32)GETARG(pptr[i]) == -1)
- {
- PUTOP(pptr[i], oINC);
- PUTARG(pptr[i], 0);
- PUTOP(pptr[i+1], oEQUZ);
- nchanges++;
- } /* end else if */
- else i++;
- break;
-
- case oNEQ :
- if (GETARG(pptr[i]) == 0)
- {
- PUTOP(pptr[i+1], oNEQZ);
- deletePcode(i);
- nchanges++;
- } /* end if */
- else if (GETARG(pptr[i]) == 1)
- {
- PUTOP(pptr[i], oDEC);
- PUTARG(pptr[i], 0);
- PUTOP(pptr[i+1], oNEQZ);
- nchanges++;
- } /* end else if */
- else if ((sint32)GETARG(pptr[i]) == -1)
- {
- PUTOP(pptr[i], oINC);
- PUTARG(pptr[i], 0);
- PUTOP(pptr[i+1], oNEQZ);
- nchanges++;
- } /* end else if */
- else i++;
- break;
-
- case oLT :
- if (GETARG(pptr[i]) == 0)
- {
- PUTOP(pptr[i+1], oLTZ);
- deletePcode(i);
- nchanges++;
- } /* end if */
- else if (GETARG(pptr[i]) == 1)
- {
- PUTOP(pptr[i], oDEC);
- PUTARG(pptr[i], 0);
- PUTOP(pptr[i+1], oLTZ);
- nchanges++;
- } /* end else if */
- else if ((sint32)GETARG(pptr[i]) == -1)
- {
- PUTOP(pptr[i], oINC);
- PUTARG(pptr[i], 0);
- PUTOP(pptr[i+1], oLTZ);
- nchanges++;
- } /* end else if */
- else i++;
- break;
-
- case oGTE :
- if (GETARG(pptr[i]) == 0)
- {
- PUTOP(pptr[i+1], oGTEZ);
- deletePcode(i);
- nchanges++;
- } /* end if */
- else if (GETARG(pptr[i]) == 1)
- {
- PUTOP(pptr[i], oDEC);
- PUTARG(pptr[i], 0);
- PUTOP(pptr[i+1], oGTEZ);
- nchanges++;
- } /* end else if */
- else if ((sint32)GETARG(pptr[i]) == -1)
- {
- PUTOP(pptr[i], oINC);
- PUTARG(pptr[i], 0);
- PUTOP(pptr[i+1], oGTEZ);
- nchanges++;
- } /* end else if */
- else i++;
- break;
-
- case oGT :
- if (GETARG(pptr[i]) == 0)
- {
- PUTOP(pptr[i+1], oGTZ);
- deletePcode(i);
- nchanges++;
- } /* end if */
- else if (GETARG(pptr[i]) == 1)
- {
- PUTOP(pptr[i], oDEC);
- PUTARG(pptr[i], 0);
- PUTOP(pptr[i+1], oGTZ);
- nchanges++;
- } /* end else if */
- else if ((sint32)GETARG(pptr[i]) == -1)
- {
- PUTOP(pptr[i], oINC);
- PUTARG(pptr[i], 0);
- PUTOP(pptr[i+1], oGTZ);
- nchanges++;
- } /* end else if */
- else i++;
- break;
-
- case oLTE :
- if (GETARG(pptr[i]) == 0)
- {
- PUTOP(pptr[i+1], oLTEZ);
- deletePcode(i);
- nchanges++;
- } /* end if */
- else if (GETARG(pptr[i]) == 1)
- {
- PUTOP(pptr[i], oDEC);
- PUTARG(pptr[i], 0);
- PUTOP(pptr[i+1], oLTEZ);
- nchanges++;
- } /* end else if */
- else if ((sint32)GETARG(pptr[i]) == -1)
- {
- PUTOP(pptr[i], oINC);
- PUTARG(pptr[i], 0);
- PUTOP(pptr[i+1], oLTEZ);
- nchanges++;
- } /* end else if */
- else i++;
- break;
-
- /* Simplify or delete condition branches on constants */
-
- case oJEQUZ :
- if (GETARG(pptr[i]) == 0)
- {
- PUTOP(pptr[i+1], oJMP);
- deletePcode(i);
- } /* end if */
- else
- deletePcodePair(i, (i+1));
- nchanges++;
- break;
-
- case oJNEQZ :
- if (GETARG(pptr[i]) != 0)
- {
- PUTOP(pptr[i+1], oJMP);
- deletePcode(i);
- } /* end if */
- else
- deletePcodePair(i, (i+1));
- nchanges++;
- break;
-
- case oJLTZ :
- if ((sint32)GETARG(pptr[i]) < 0)
- {
- PUTOP(pptr[i+1], oJMP);
- deletePcode(i);
- } /* end if */
- else
- deletePcodePair(i, (i+1));
- nchanges++;
- break;
-
- case oJGTEZ :
- if ((sint32)GETARG(pptr[i]) >= 0)
- {
- PUTOP(pptr[i+1], oJMP);
- deletePcode(i);
- } /* end if */
- else
- deletePcodePair(i, (i+1));
- nchanges++;
- break;
-
- case oJGTZ :
- if (GETARG(pptr[i]) > 0)
- {
- PUTOP(pptr[i+1], oJMP);
- deletePcode(i);
- } /* end if */
- else
- deletePcodePair(i, (i+1));
- nchanges++;
- break;
-
- case oJLTEZ :
- if (GETARG(pptr[i]) <= 0)
- {
- PUTOP(pptr[i+1], oJMP);
- deletePcode(i);
- } /* end if */
- else
- deletePcodePair(i, (i+1));
- nchanges++;
- break;
-
- default :
- i++;
- break;
- } /* end switch */
- } /* end if */
-
- /* Delete multiple modifications of DSEG pointer */
-
- else if (GETOP(pptr[i]) == oINDS)
- {
- if (GETOP(pptr[i+1]) == oINDS)
- {
- PUTARG(pptr[i], GETARG(pptr[i] + GETARG(pptr[i+1])));
- deletePcode(i+1);
- } /* end if */
- else i++;
- } /* end else if */
- else i++;
- } /* end while */
-
- return (nchanges);
-
-} /* end unaryOptimize */
-
-/**********************************************************************/
-
-int binaryOptimize(void)
-{
- register int i;
- int nchanges = 0;
-
- TRACE(stderr, "[binaryOptimize]");
-
- /* At least two pcodes are needed to perform the following binary */
- /* operator optimizations */
-
- i = 0;
- while (i < nops-2)
- {
- if (GETOP(pptr[i]) == oPUSH)
- {
- if (GETOP(pptr[i+1]) == oPUSH)
- {
- switch (GETOP(pptr[i+2]))
- {
- case oADD :
- PUTARG(pptr[i], GETARG(pptr[i]) + GETARG(pptr[i+1]));
- deletePcodePair((i+1), (i+2));
- nchanges++;
- break;
-
- case oSUB :
- PUTARG(pptr[i], GETARG(pptr[i]) - GETARG(pptr[i+1]));
- deletePcodePair((i+1), (i+2));
- nchanges++;
- break;
-
- case oMUL :
- PUTARG(pptr[i], GETARG(pptr[i]) * GETARG(pptr[i+1]));
- deletePcodePair((i+1), (i+2));
- nchanges++;
- break;
-
- case oDIV :
- PUTARG(pptr[i], GETARG(pptr[i]) / (sint32)GETARG(pptr[i+1]));
- deletePcodePair((i+1), (i+2));
- nchanges++;
- break;
-
- case oMOD :
- PUTARG(pptr[i], GETARG(pptr[i]) % GETARG(pptr[i+1]));
- deletePcodePair((i+1), (i+2));
- nchanges++;
- break;
-
- case oSLL :
- PUTARG(pptr[i], GETARG(pptr[i]) << GETARG(pptr[i+1]));
- deletePcodePair((i+1), (i+2));
- nchanges++;
- break;
-
- case oSRL :
- PUTARG(pptr[i], GETARG(pptr[i]) >> GETARG(pptr[i+1]));
- deletePcodePair((i+1), (i+2));
- nchanges++;
- break;
-
- case oSRA :
- PUTARG(pptr[i], (sint32)GETARG(pptr[i]) >> GETARG(pptr[i+1]));
- deletePcodePair((i+1), (i+2));
- nchanges++;
- break;
-
- case oOR :
- PUTARG(pptr[i], GETARG(pptr[i]) | GETARG(pptr[i+1]));
- deletePcodePair((i+1), (i+2));
- nchanges++;
- break;
-
- case oAND :
- PUTARG(pptr[i], GETARG(pptr[i]) & GETARG(pptr[i+1]));
- deletePcodePair((i+1), (i+2));
- nchanges++;
- break;
-
- case oEQU :
- if (GETARG(pptr[i]) == GETARG(pptr[i+1]))
- PUTARG(pptr[i], -1);
- else
- PUTARG(pptr[i], 0);
- deletePcodePair((i+1), (i+2));
- nchanges++;
- break;
-
- case oNEQ :
- if (GETARG(pptr[i]) != GETARG(pptr[i+1]))
- PUTARG(pptr[i], -1);
- else
- PUTARG(pptr[i], 0);
- deletePcodePair((i+1), (i+2));
- nchanges++;
- break;
-
- case oLT :
- if ((sint32)GETARG(pptr[i]) < (sint32)GETARG(pptr[i+1]))
- PUTARG(pptr[i], -1);
- else
- PUTARG(pptr[i], 0);
- deletePcodePair((i+1), (i+2));
- nchanges++;
- break;
-
- case oGTE :
- if ((sint32)GETARG(pptr[i]) >= (sint32)GETARG(pptr[i+1]))
- PUTARG(pptr[i], -1);
- else
- PUTARG(pptr[i], 0);
- deletePcodePair((i+1), (i+2));
- nchanges++;
- break;
-
- case oGT :
- if ((sint32)GETARG(pptr[i]) > (sint32)GETARG(pptr[i+1]))
- PUTARG(pptr[i], -1);
- else
- PUTARG(pptr[i], 0);
- deletePcodePair((i+1), (i+2));
- nchanges++;
- break;
-
- case oLTE :
- if ((sint32)GETARG(pptr[i]) <= (sint32)GETARG(pptr[i+1]))
- PUTARG(pptr[i], -1);
- else
- PUTARG(pptr[i], 0);
- deletePcodePair((i+1), (i+2));
- nchanges++;
- break;
-
- default :
- i++;
- break;
- } /* end switch */
- } /* end if */
-
- /* A single (constant) pcode is sufficient to perform the */
- /* following binary operator optimizations */
-
- else if ((GETOP(pptr[i+1]) == oLDSH) || (GETOP(pptr[i+1]) == oLDSB) ||
- (GETOP(pptr[i+1]) == oLAS) || (GETOP(pptr[i+1]) == oLAC))
- {
- switch (GETOP(pptr[i+2]))
- {
- case oADD :
- if (GETARG(pptr[i]) == 0)
- {
- deletePcodePair(i, (i+2));
- nchanges++;
- } /* end if */
- else if (GETARG(pptr[i]) == 1)
- {
- PUTOP(pptr[i+2], oINC);
- deletePcode(i);
- nchanges++;
- } /* end else if */
- else if (GETARG(pptr[i]) == ARGONES)
- {
- PUTOP(pptr[i+2], oDEC);
- deletePcode(i);
- nchanges++;
- } /* end else if */
- else i++;
- break;
-
- case oSUB :
- if (GETARG(pptr[i]) == 0)
- {
- PUTOP(pptr[i], oNEG);
- deletePcode(i);
- nchanges++;
- } /* end if */
- else i++;
- break;
-
- case oMUL :
- {
- sint32 stmp32 = 0;
- switch (GETARG(pptr[i]))
- {
- case 1 :
- deletePcodePair(i, (i+2));
- nchanges++;
- break;
- case 16384 : stmp32++;
- case 8192 : stmp32++;
- case 4096 : stmp32++;
- case 2048 : stmp32++;
- case 1024 : stmp32++;
- case 512 : stmp32++;
- case 256 : stmp32++;
- case 128 : stmp32++;
- case 64 : stmp32++;
- case 32 : stmp32++;
- case 16 : stmp32++;
- case 8 : stmp32++;
- case 4 : stmp32++;
- case 2 : stmp32++;
- PUTOP(pptr[i], GETOP(pptr[i+1]));
- PUTARG(pptr[i], GETARG(pptr[i+1]));
- PUTOP(pptr[i+1], oPUSH);
- PUTARG(pptr[i+1], stmp32);
- PUTOP(pptr[i+2], oSLL);
- nchanges++;
- i++;
- break;
-
- default :
- i++;
- break;
- }
- }
- break;
-
- case oOR :
- if (GETARG(pptr[i]) == 0)
- {
- deletePcodePair(i, (i+2));
- nchanges++;
- } /* end if */
- else i++;
- break;
-
- case oAND :
- if (GETARG(pptr[i]) == ARGONES)
- {
- deletePcodePair(i, (i+2));
- nchanges++;
- } /* end if */
- else i++;
- break;
-
- case oEQU :
- if (GETARG(pptr[i]) == 0)
- {
- PUTOP(pptr[i+2], oEQUZ);
- deletePcode(i);
- nchanges++;
- } /* end if */
- else i++;
- break;
-
- case oNEQ :
- if (GETARG(pptr[i]) == 0)
- {
- PUTOP(pptr[i+2], oNEQZ);
- deletePcode(i);
- nchanges++;
- } /* end if */
- else i++;
- break;
-
- case oLT :
- if (GETARG(pptr[i]) == 0)
- {
- PUTOP(pptr[i+2], oGTEZ);
- deletePcode(i);
- nchanges++;
- } /* end if */
- else i++;
- break;
-
- case oGTE :
- if (GETARG(pptr[i]) == 0)
- {
- PUTOP(pptr[i+2], oLTZ);
- deletePcode(i);
- nchanges++;
- } /* end if */
- else i++;
- break;
-
- case oGT :
- if (GETARG(pptr[i]) == 0)
- {
- PUTOP(pptr[i+2], oLTEZ);
- deletePcode(i);
- nchanges++;
- } /* end if */
- else i++;
- break;
-
- case oLTE :
- if (GETARG(pptr[i]) == 0)
- {
- PUTOP(pptr[i+2], oGTZ);
- deletePcode(i);
- nchanges++;
- } /* end if */
- else i++;
- break;
-
- default :
- i++;
- break;
-
- } /* end switch */
- } /* end else if */
- else i++;
- } /* end if */
-
- /* Misc improvements on binary operators */
-
- else if (GETOP(pptr[i]) == oNEG)
- {
- /* Negation followed by add is subtraction */
-
- if (GETOP(pptr[i+1]) == oADD)
- {
- PUTOP(pptr[i+1], oSUB);
- deletePcode(i);
- nchanges++;
- }
-
- /* Negation followed by subtraction is addition */
-
- else if (GETOP(pptr[i]) == oSUB)
- {
- PUTOP(pptr[i+1], 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 "pinsn32.h" + +#include "paslib.h" +#include "popt.h" +#include "polocal.h" +#include "pcopt.h" + +/**********************************************************************/ + +int unaryOptimize(void) +{ + register uint32_t temp; + register int i; + int nchanges = 0; + + 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 (GETOP(pptr[i]) == oPUSH) + { + switch (GETOP(pptr[i+1])) + { + /* Delete unary operators on constants */ + case oNEG : + PUTARG(pptr[i], -GETARG(pptr[i])); + deletePcode(i+1); + nchanges++; + break; + + case oABS : + if ((int32_t)GETARG(pptr[i]) < 0) + PUTARG(pptr[i], -(int32_t)GETARG(pptr[i])); + deletePcode(i+1); + nchanges++; + break; + + case oINC : + PUTARG(pptr[i], GETARG(pptr[i]) + 1); + deletePcode(i+1); + nchanges++; + break; + + case oDEC : + PUTARG(pptr[i], GETARG(pptr[i]) - 1); + deletePcode(i+1); + nchanges++; + break; + + case oNOT : + PUTARG(pptr[i], ~GETARG(pptr[i])); + PUTARG(pptr[i], ~(GETARG(pptr[i]))); + deletePcode(i+1); + nchanges++; + break; + + /* Simplify binary operations on constants */ + + case oADD : + if (GETARG(pptr[i]) == 0) + { + deletePcodePair(i, (i+1)); + nchanges++; + } /* end if */ + else if (GETARG(pptr[i]) == 1) + { + PUTOP(pptr[i+1], oINC); + deletePcode(i); + nchanges++; + } /* end else if */ + else if (GETARG(pptr[i]) == ARGONES) + { + PUTOP(pptr[i+1], oDEC); + deletePcode(i); + nchanges++; + } /* end else if */ + else i++; + break; + + case oSUB : + if (GETARG(pptr[i]) == 0) + { + deletePcodePair(i, (i+1)); + nchanges++; + } /* end if */ + else if (GETARG(pptr[i]) == 1) + { + PUTOP(pptr[i+1], oDEC); + deletePcode(i); + nchanges++; + } /* end else if */ + else if (GETARG(pptr[i]) == ARGONES) + { + PUTOP(pptr[i+1], oINC); + deletePcode(i); + nchanges++; + } /* end else if */ + else i++; + break; + + case oMUL : + case oDIV : + temp = 0; + switch (GETARG(pptr[i])) + { + 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++; + PUTARG(pptr[i], temp); + if (GETOP(pptr[i+1]) == oMUL) + PUTOP(pptr[i+1], oSLL); + else + PUTOP(pptr[i+1], oSRA); + nchanges++; + i++; + break; + + default : + i++; + break; + } /* end switch */ + break; + + case oSLL : + case oSRL : + case oSRA : + case oOR : + if (GETARG(pptr[i]) == 0) + { + deletePcodePair(i, (i+1)); + nchanges++; + } /* end if */ + else i++; + break; + + case oAND : + if (GETARG(pptr[i]) == ARGONES) + { + deletePcodePair(i, (i+1)); + nchanges++; + } /* end if */ + else i++; + break; + + /* Delete comparisons of constants to zero */ + + case oEQUZ : + if (GETARG(pptr[i]) == 0) + PUTARG(pptr[i], -1); + else + PUTARG(pptr[i], 0); + deletePcode(i+1); + nchanges++; + break; + + case oNEQZ : + if (GETARG(pptr[i]) != 0) + PUTARG(pptr[i], -1); + else + PUTARG(pptr[i], 0); + deletePcode(i+1); + nchanges++; + break; + + case oLTZ : + if ((int32_t)GETARG(pptr[i]) < 0) + PUTARG(pptr[i], -1); + else + PUTARG(pptr[i], 0); + deletePcode(i+1); + nchanges++; + break; + + case oGTEZ : + if ((int32_t)GETARG(pptr[i]) >= 0) + PUTARG(pptr[i], -1); + else + PUTARG(pptr[i], 0); + deletePcode(i+1); + nchanges++; + break; + + case oGTZ : + if (GETARG(pptr[i]) > 0) + PUTARG(pptr[i], -1); + else + PUTARG(pptr[i], 0); + deletePcode(i+1); + nchanges++; + break; + + case oLTEZ : + if (GETARG(pptr[i]) <= 0) + PUTARG(pptr[i], -1); + else + PUTARG(pptr[i], 0); + deletePcode(i+1); + nchanges++; + break; + + /* Simplify comparisons with certain constants */ + + case oEQU : + if (GETARG(pptr[i]) == 0) + { + PUTOP(pptr[i+1],oEQUZ); + deletePcode(i); + nchanges++; + } /* end if */ + else if (GETARG(pptr[i]) == 1) + { + PUTOP(pptr[i], oDEC); + PUTARG(pptr[i], 0); + PUTOP(pptr[i+1], oEQUZ); + nchanges++; + } /* end else if */ + else if ((int32_t)GETARG(pptr[i]) == -1) + { + PUTOP(pptr[i], oINC); + PUTARG(pptr[i], 0); + PUTOP(pptr[i+1], oEQUZ); + nchanges++; + } /* end else if */ + else i++; + break; + + case oNEQ : + if (GETARG(pptr[i]) == 0) + { + PUTOP(pptr[i+1], oNEQZ); + deletePcode(i); + nchanges++; + } /* end if */ + else if (GETARG(pptr[i]) == 1) + { + PUTOP(pptr[i], oDEC); + PUTARG(pptr[i], 0); + PUTOP(pptr[i+1], oNEQZ); + nchanges++; + } /* end else if */ + else if ((int32_t)GETARG(pptr[i]) == -1) + { + PUTOP(pptr[i], oINC); + PUTARG(pptr[i], 0); + PUTOP(pptr[i+1], oNEQZ); + nchanges++; + } /* end else if */ + else i++; + break; + + case oLT : + if (GETARG(pptr[i]) == 0) + { + PUTOP(pptr[i+1], oLTZ); + deletePcode(i); + nchanges++; + } /* end if */ + else if (GETARG(pptr[i]) == 1) + { + PUTOP(pptr[i], oDEC); + PUTARG(pptr[i], 0); + PUTOP(pptr[i+1], oLTZ); + nchanges++; + } /* end else if */ + else if ((int32_t)GETARG(pptr[i]) == -1) + { + PUTOP(pptr[i], oINC); + PUTARG(pptr[i], 0); + PUTOP(pptr[i+1], oLTZ); + nchanges++; + } /* end else if */ + else i++; + break; + + case oGTE : + if (GETARG(pptr[i]) == 0) + { + PUTOP(pptr[i+1], oGTEZ); + deletePcode(i); + nchanges++; + } /* end if */ + else if (GETARG(pptr[i]) == 1) + { + PUTOP(pptr[i], oDEC); + PUTARG(pptr[i], 0); + PUTOP(pptr[i+1], oGTEZ); + nchanges++; + } /* end else if */ + else if ((int32_t)GETARG(pptr[i]) == -1) + { + PUTOP(pptr[i], oINC); + PUTARG(pptr[i], 0); + PUTOP(pptr[i+1], oGTEZ); + nchanges++; + } /* end else if */ + else i++; + break; + + case oGT : + if (GETARG(pptr[i]) == 0) + { + PUTOP(pptr[i+1], oGTZ); + deletePcode(i); + nchanges++; + } /* end if */ + else if (GETARG(pptr[i]) == 1) + { + PUTOP(pptr[i], oDEC); + PUTARG(pptr[i], 0); + PUTOP(pptr[i+1], oGTZ); + nchanges++; + } /* end else if */ + else if ((int32_t)GETARG(pptr[i]) == -1) + { + PUTOP(pptr[i], oINC); + PUTARG(pptr[i], 0); + PUTOP(pptr[i+1], oGTZ); + nchanges++; + } /* end else if */ + else i++; + break; + + case oLTE : + if (GETARG(pptr[i]) == 0) + { + PUTOP(pptr[i+1], oLTEZ); + deletePcode(i); + nchanges++; + } /* end if */ + else if (GETARG(pptr[i]) == 1) + { + PUTOP(pptr[i], oDEC); + PUTARG(pptr[i], 0); + PUTOP(pptr[i+1], oLTEZ); + nchanges++; + } /* end else if */ + else if ((int32_t)GETARG(pptr[i]) == -1) + { + PUTOP(pptr[i], oINC); + PUTARG(pptr[i], 0); + PUTOP(pptr[i+1], oLTEZ); + nchanges++; + } /* end else if */ + else i++; + break; + + /* Simplify or delete condition branches on constants */ + + case oJEQUZ : + if (GETARG(pptr[i]) == 0) + { + PUTOP(pptr[i+1], oJMP); + deletePcode(i); + } /* end if */ + else + deletePcodePair(i, (i+1)); + nchanges++; + break; + + case oJNEQZ : + if (GETARG(pptr[i]) != 0) + { + PUTOP(pptr[i+1], oJMP); + deletePcode(i); + } /* end if */ + else + deletePcodePair(i, (i+1)); + nchanges++; + break; + + case oJLTZ : + if ((int32_t)GETARG(pptr[i]) < 0) + { + PUTOP(pptr[i+1], oJMP); + deletePcode(i); + } /* end if */ + else + deletePcodePair(i, (i+1)); + nchanges++; + break; + + case oJGTEZ : + if ((int32_t)GETARG(pptr[i]) >= 0) + { + PUTOP(pptr[i+1], oJMP); + deletePcode(i); + } /* end if */ + else + deletePcodePair(i, (i+1)); + nchanges++; + break; + + case oJGTZ : + if (GETARG(pptr[i]) > 0) + { + PUTOP(pptr[i+1], oJMP); + deletePcode(i); + } /* end if */ + else + deletePcodePair(i, (i+1)); + nchanges++; + break; + + case oJLTEZ : + if (GETARG(pptr[i]) <= 0) + { + PUTOP(pptr[i+1], oJMP); + deletePcode(i); + } /* end if */ + else + deletePcodePair(i, (i+1)); + nchanges++; + break; + + default : + i++; + break; + } /* end switch */ + } /* end if */ + + /* Delete multiple modifications of DSEG pointer */ + + else if (GETOP(pptr[i]) == oINDS) + { + if (GETOP(pptr[i+1]) == oINDS) + { + PUTARG(pptr[i], GETARG(pptr[i] + GETARG(pptr[i+1]))); + deletePcode(i+1); + } /* end if */ + else i++; + } /* end else if */ + else i++; + } /* end while */ + + return (nchanges); + +} /* end unaryOptimize */ + +/**********************************************************************/ + +int binaryOptimize(void) +{ + register int i; + int nchanges = 0; + + TRACE(stderr, "[binaryOptimize]"); + + /* At least two pcodes are needed to perform the following binary */ + /* operator optimizations */ + + i = 0; + while (i < nops-2) + { + if (GETOP(pptr[i]) == oPUSH) + { + if (GETOP(pptr[i+1]) == oPUSH) + { + switch (GETOP(pptr[i+2])) + { + case oADD : + PUTARG(pptr[i], GETARG(pptr[i]) + GETARG(pptr[i+1])); + deletePcodePair((i+1), (i+2)); + nchanges++; + break; + + case oSUB : + PUTARG(pptr[i], GETARG(pptr[i]) - GETARG(pptr[i+1])); + deletePcodePair((i+1), (i+2)); + nchanges++; + break; + + case oMUL : + PUTARG(pptr[i], GETARG(pptr[i]) * GETARG(pptr[i+1])); + deletePcodePair((i+1), (i+2)); + nchanges++; + break; + + case oDIV : + PUTARG(pptr[i], GETARG(pptr[i]) / (int32_t)GETARG(pptr[i+1])); + deletePcodePair((i+1), (i+2)); + nchanges++; + break; + + case oMOD : + PUTARG(pptr[i], GETARG(pptr[i]) % GETARG(pptr[i+1])); + deletePcodePair((i+1), (i+2)); + nchanges++; + break; + + case oSLL : + PUTARG(pptr[i], GETARG(pptr[i]) << GETARG(pptr[i+1])); + deletePcodePair((i+1), (i+2)); + nchanges++; + break; + + case oSRL : + PUTARG(pptr[i], GETARG(pptr[i]) >> GETARG(pptr[i+1])); + deletePcodePair((i+1), (i+2)); + nchanges++; + break; + + case oSRA : + PUTARG(pptr[i], (int32_t)GETARG(pptr[i]) >> GETARG(pptr[i+1])); + deletePcodePair((i+1), (i+2)); + nchanges++; + break; + + case oOR : + PUTARG(pptr[i], GETARG(pptr[i]) | GETARG(pptr[i+1])); + deletePcodePair((i+1), (i+2)); + nchanges++; + break; + + case oAND : + PUTARG(pptr[i], GETARG(pptr[i]) & GETARG(pptr[i+1])); + deletePcodePair((i+1), (i+2)); + nchanges++; + break; + + case oEQU : + if (GETARG(pptr[i]) == GETARG(pptr[i+1])) + PUTARG(pptr[i], -1); + else + PUTARG(pptr[i], 0); + deletePcodePair((i+1), (i+2)); + nchanges++; + break; + + case oNEQ : + if (GETARG(pptr[i]) != GETARG(pptr[i+1])) + PUTARG(pptr[i], -1); + else + PUTARG(pptr[i], 0); + deletePcodePair((i+1), (i+2)); + nchanges++; + break; + + case oLT : + if ((int32_t)GETARG(pptr[i]) < (int32_t)GETARG(pptr[i+1])) + PUTARG(pptr[i], -1); + else + PUTARG(pptr[i], 0); + deletePcodePair((i+1), (i+2)); + nchanges++; + break; + + case oGTE : + if ((int32_t)GETARG(pptr[i]) >= (int32_t)GETARG(pptr[i+1])) + PUTARG(pptr[i], -1); + else + PUTARG(pptr[i], 0); + deletePcodePair((i+1), (i+2)); + nchanges++; + break; + + case oGT : + if ((int32_t)GETARG(pptr[i]) > (int32_t)GETARG(pptr[i+1])) + PUTARG(pptr[i], -1); + else + PUTARG(pptr[i], 0); + deletePcodePair((i+1), (i+2)); + nchanges++; + break; + + case oLTE : + if ((int32_t)GETARG(pptr[i]) <= (int32_t)GETARG(pptr[i+1])) + PUTARG(pptr[i], -1); + else + PUTARG(pptr[i], 0); + deletePcodePair((i+1), (i+2)); + nchanges++; + break; + + default : + i++; + break; + } /* end switch */ + } /* end if */ + + /* A single (constant) pcode is sufficient to perform the */ + /* following binary operator optimizations */ + + else if ((GETOP(pptr[i+1]) == oLDSH) || (GETOP(pptr[i+1]) == oLDSB) || + (GETOP(pptr[i+1]) == oLAS) || (GETOP(pptr[i+1]) == oLAC)) + { + switch (GETOP(pptr[i+2])) + { + case oADD : + if (GETARG(pptr[i]) == 0) + { + deletePcodePair(i, (i+2)); + nchanges++; + } /* end if */ + else if (GETARG(pptr[i]) == 1) + { + PUTOP(pptr[i+2], oINC); + deletePcode(i); + nchanges++; + } /* end else if */ + else if (GETARG(pptr[i]) == ARGONES) + { + PUTOP(pptr[i+2], oDEC); + deletePcode(i); + nchanges++; + } /* end else if */ + else i++; + break; + + case oSUB : + if (GETARG(pptr[i]) == 0) + { + PUTOP(pptr[i], oNEG); + deletePcode(i); + nchanges++; + } /* end if */ + else i++; + break; + + case oMUL : + { + int32_t stmp32 = 0; + switch (GETARG(pptr[i])) + { + case 1 : + deletePcodePair(i, (i+2)); + nchanges++; + break; + case 16384 : stmp32++; + case 8192 : stmp32++; + case 4096 : stmp32++; + case 2048 : stmp32++; + case 1024 : stmp32++; + case 512 : stmp32++; + case 256 : stmp32++; + case 128 : stmp32++; + case 64 : stmp32++; + case 32 : stmp32++; + case 16 : stmp32++; + case 8 : stmp32++; + case 4 : stmp32++; + case 2 : stmp32++; + PUTOP(pptr[i], GETOP(pptr[i+1])); + PUTARG(pptr[i], GETARG(pptr[i+1])); + PUTOP(pptr[i+1], oPUSH); + PUTARG(pptr[i+1], stmp32); + PUTOP(pptr[i+2], oSLL); + nchanges++; + i++; + break; + + default : + i++; + break; + } + } + break; + + case oOR : + if (GETARG(pptr[i]) == 0) + { + deletePcodePair(i, (i+2)); + nchanges++; + } /* end if */ + else i++; + break; + + case oAND : + if (GETARG(pptr[i]) == ARGONES) + { + deletePcodePair(i, (i+2)); + nchanges++; + } /* end if */ + else i++; + break; + + case oEQU : + if (GETARG(pptr[i]) == 0) + { + PUTOP(pptr[i+2], oEQUZ); + deletePcode(i); + nchanges++; + } /* end if */ + else i++; + break; + + case oNEQ : + if (GETARG(pptr[i]) == 0) + { + PUTOP(pptr[i+2], oNEQZ); + deletePcode(i); + nchanges++; + } /* end if */ + else i++; + break; + + case oLT : + if (GETARG(pptr[i]) == 0) + { + PUTOP(pptr[i+2], oGTEZ); + deletePcode(i); + nchanges++; + } /* end if */ + else i++; + break; + + case oGTE : + if (GETARG(pptr[i]) == 0) + { + PUTOP(pptr[i+2], oLTZ); + deletePcode(i); + nchanges++; + } /* end if */ + else i++; + break; + + case oGT : + if (GETARG(pptr[i]) == 0) + { + PUTOP(pptr[i+2], oLTEZ); + deletePcode(i); + nchanges++; + } /* end if */ + else i++; + break; + + case oLTE : + if (GETARG(pptr[i]) == 0) + { + PUTOP(pptr[i+2], oGTZ); + deletePcode(i); + nchanges++; + } /* end if */ + else i++; + break; + + default : + i++; + break; + + } /* end switch */ + } /* end else if */ + else i++; + } /* end if */ + + /* Misc improvements on binary operators */ + + else if (GETOP(pptr[i]) == oNEG) + { + /* Negation followed by add is subtraction */ + + if (GETOP(pptr[i+1]) == oADD) + { + PUTOP(pptr[i+1], oSUB); + deletePcode(i); + nchanges++; + } + + /* Negation followed by subtraction is addition */ + + else if (GETOP(pptr[i]) == oSUB) + { + PUTOP(pptr[i+1], oADD); + deletePcode(i); + nchanges++; + } + else i++; + } + else i++; + } /* end while */ + + return (nchanges); + +} /* end binaryOptimize */ + +/**********************************************************************/ diff --git a/misc/pascal/insn32/popt/pfopt.c b/misc/pascal/insn32/popt/pfopt.c index de07400bf..cf71fe005 100644 --- a/misc/pascal/insn32/popt/pfopt.c +++ b/misc/pascal/insn32/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> @@ -85,7 +86,7 @@ static void pass1(poffHandle_t poffHandle, poffProgHandle_t poffProgHandle) { OPTYPE op; - uint32 pc; + uint32_t pc; int opsize; int fileno = 0; @@ -106,22 +107,22 @@ static void pass1(poffHandle_t poffHandle, poffProgHandle_t poffProgHandle) { opsize = insn_GetOpCode(poffHandle, &op); if (GETOP(&op) == oLABEL) - { - poffAddToDefinedLabelTable(GETARG(&op), pc); - } + { + poffAddToDefinedLabelTable(GETARG(&op), pc); + } else if (GETOP(&op) == oINCLUDE) - { - fileno = GETARG(&op); - } + { + fileno = GETARG(&op); + } else if (GETOP(&op) == oLINE) - { - poffAddLineNumber(poffHandle, GETARG(&op), fileno, pc); - } + { + poffAddLineNumber(poffHandle, GETARG(&op), fileno, pc); + } else - { - insn_AddTmpOpCode(poffProgHandle, &op); - pc += opsize; - } + { + insn_AddTmpOpCode(poffProgHandle, &op); + pc += opsize; + } } while (GETOP(&op) != oEND); @@ -135,8 +136,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. @@ -158,49 +159,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 lavel 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 lavel 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); @@ -223,9 +224,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. @@ -248,133 +249,133 @@ static void pass3(poffHandle_t poffHandle, poffProgHandle_t poffProgHandle) { opsize = insn_GetOpCode(poffHandle, &op); switch (GETOP(&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(GETARG(&op)); - 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. - */ - - PUTARG(&op, 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(GETARG(&op)); - if (value >= 0) - { - /* Use the value zero now */ - - PUTARG(&op, 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", - GETARG(&op)); - 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(GETARG(&op)); - 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. - */ - - PUTARG(&op, value); - } - else - { - DEBUG(stdout, "Failed to find jump label L%04x\n", - GETARG(&op)); - 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(GETARG(&op)); + 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. + */ + + PUTARG(&op, 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(GETARG(&op)); + if (value >= 0) + { + /* Use the value zero now */ + + PUTARG(&op, 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", + GETARG(&op)); + 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(GETARG(&op)); + 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. + */ + + PUTARG(&op, value); + } + else + { + DEBUG(stdout, "Failed to find jump label L%04x\n", + GETARG(&op)); + 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. @@ -405,13 +406,13 @@ static void pass4(poffHandle_t poffHandle) while ((pDebugInfo = poffGetDebugFuncInfo(poffHandle)) != NULL) { if (!pDebugInfoHead) - { - pDebugInfoHead = pDebugInfo; - } + { + pDebugInfoHead = pDebugInfo; + } else - { - pDebugInfoTail->next = pDebugInfo; - } + { + pDebugInfoTail->next = pDebugInfo; + } pDebugInfoTail = pDebugInfo; } @@ -423,17 +424,17 @@ static void pass4(poffHandle_t poffHandle) * because there can be no jumps into a unit file. */ - sint32 value = poffGetPcForDefinedLabel(pDebugInfo->value); + int32_t value = poffGetPcForDefinedLabel(pDebugInfo->value); if (value >= 0) - { - /* Yes... replace the label reference with a text section offset. */ + { + /* Yes... replace the label reference with a text section offset. */ - pDebugInfo->value = value; - } + pDebugInfo->value = value; + } else - { - fatal(ePOFFCONFUSION); - } + { + fatal(ePOFFCONFUSION); + } } /* Then put all of the debug info back into the POFF object */ @@ -460,9 +461,9 @@ static void pass4(poffHandle_t poffHandle) static void pass5(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? @@ -481,9 +482,9 @@ static void pass5(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/insn32/popt/plopt.c b/misc/pascal/insn32/popt/plopt.c index d58f1a554..b0a469533 100644 --- a/misc/pascal/insn32/popt/plopt.c +++ b/misc/pascal/insn32/popt/plopt.c @@ -1,214 +1,215 @@ -/**********************************************************************
- * 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 "pinsn32.h"
-
-#include "popt.h"
-#include "polocal.h"
-#include "plopt.h"
-
-/**********************************************************************/
-
-int LoadOptimize(void)
-{
- uint32 val;
- int nchanges = 0;
- register int i;
-
- TRACE(stderr, "[LoadOptimize]");
-
- /* At least two pcodes are need to perform Load optimizations */
-
- i = 0;
- while (i < nops-1)
- {
- switch (GETOP(pptr[i]))
- {
- /* Eliminate duplicate loads */
-
- case oLDSH :
- if ((GETOP(pptr[i+1]) == oLDSH) &&
- (GETARG(pptr[i+1]) == GETARG(pptr[i])))
- {
- PUTOP(pptr[i+1], oDUP);
- PUTARG(pptr[i+1], 0);
- nchanges++;
- i += 2;
- } /* end if */
- else i++;
- break;
-
- /* Convert loads indexed by a constant to unindexed loads */
-
- case oPUSH :
- /* Get the index value */
-
- val = (sint32)GETARG(pptr[i]);
-
- /* If the following instruction is a load, add the constant
- * index value to the address and switch the opcode to the
- * unindexed form.
- */
-
- if (GETOP(pptr[i+1]) == oLDSXH)
- {
- PUTOP(pptr[i+1], oLDSH);
- val += GETARG(pptr[i+1]);
- PUTARG(pptr[i+1], val);
- deletePcode (i);
- nchanges++;
- } /* end if */
- else if (GETOP(pptr[i+1]) == oLASX)
- {
- PUTOP(pptr[i+1], oLAS);
- val += GETARG(pptr[i+1]);
- PUTARG(pptr[i+1], val);
- deletePcode (i);
- nchanges++;
- } /* end else if */
- else if (GETOP(pptr[i+1]) == oLDSXB)
- {
- PUTOP(pptr[i+1], oLDSB);
- val += GETARG(pptr[i+1]);
- PUTARG(pptr[i+1], val);
- deletePcode (i);
- nchanges++;
- } /* end if */
- else if (GETOP(pptr[i+1]) == oLDSXM)
- {
- PUTOP(pptr[i+1], oLDSM);
- val += GETARG(pptr[i+1]);
- PUTARG(pptr[i+1], val);
- deletePcode (i);
- nchanges++;
- } /* end if */
- else i++;
- break;
-
- default :
- i++;
- break;
- } /* end switch */
- } /* end while */
- return (nchanges);
-} /* end LoadOptimize */
-
-/**********************************************************************/
-int StoreOptimize (void)
-{
- uint32 val;
- int nchanges = 0;
- register int i;
-
- TRACE(stderr, "[StoreOptimize]");
-
- /* At least two pcodes are need to perform the following Store */
- /* optimizations */
-
- i = 0;
- while (i < nops-1)
- {
- switch (GETOP(pptr[i]))
- {
- /* Eliminate store followed by load */
-
- case oSTSH :
- if ((GETOP(pptr[i+1]) == oLDSH) &&
- (GETARG(pptr[i+1]) == GETARG(pptr[i])))
- {
- PUTOP(pptr[i+1], oSTSH);
- PUTOP(pptr[i], oDUP);
- PUTARG(pptr[i], 0);
- nchanges++;
- i += 2;
- } /* end if */
- else i++;
- break;
-
- /* Convert stores indexed by a constant to unindexed stores */
- case oPUSH :
- /* Get the index value */
-
- val = (sint32)GETARG(pptr[i]);
-
- /* 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 (GETOP(pptr[i+2]) == oSTSXH)
- {
- PUTOP(pptr[i+2], oSTSH);
- val += GETARG(pptr[i+2]);
- PUTARG(pptr[i+2], val);
- deletePcode (i);
- nchanges++;
- } /* end if */
- else if (GETOP(pptr[i+2]) == oSTSXB)
- {
- PUTOP(pptr[i+2], oSTSB);
- val += GETARG(pptr[i+2]);
- PUTARG(pptr[i+2], val);
- 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 "pinsn32.h" + +#include "popt.h" +#include "polocal.h" +#include "plopt.h" + +/**********************************************************************/ + +int LoadOptimize(void) +{ + uint32_t val; + int nchanges = 0; + register int i; + + TRACE(stderr, "[LoadOptimize]"); + + /* At least two pcodes are need to perform Load optimizations */ + + i = 0; + while (i < nops-1) + { + switch (GETOP(pptr[i])) + { + /* Eliminate duplicate loads */ + + case oLDSH : + if ((GETOP(pptr[i+1]) == oLDSH) && + (GETARG(pptr[i+1]) == GETARG(pptr[i]))) + { + PUTOP(pptr[i+1], oDUP); + PUTARG(pptr[i+1], 0); + nchanges++; + i += 2; + } /* end if */ + else i++; + break; + + /* Convert loads indexed by a constant to unindexed loads */ + + case oPUSH : + /* Get the index value */ + + val = (int32_t)GETARG(pptr[i]); + + /* If the following instruction is a load, add the constant + * index value to the address and switch the opcode to the + * unindexed form. + */ + + if (GETOP(pptr[i+1]) == oLDSXH) + { + PUTOP(pptr[i+1], oLDSH); + val += GETARG(pptr[i+1]); + PUTARG(pptr[i+1], val); + deletePcode (i); + nchanges++; + } /* end if */ + else if (GETOP(pptr[i+1]) == oLASX) + { + PUTOP(pptr[i+1], oLAS); + val += GETARG(pptr[i+1]); + PUTARG(pptr[i+1], val); + deletePcode (i); + nchanges++; + } /* end else if */ + else if (GETOP(pptr[i+1]) == oLDSXB) + { + PUTOP(pptr[i+1], oLDSB); + val += GETARG(pptr[i+1]); + PUTARG(pptr[i+1], val); + deletePcode (i); + nchanges++; + } /* end if */ + else if (GETOP(pptr[i+1]) == oLDSXM) + { + PUTOP(pptr[i+1], oLDSM); + val += GETARG(pptr[i+1]); + PUTARG(pptr[i+1], val); + deletePcode (i); + nchanges++; + } /* end if */ + else i++; + break; + + default : + i++; + break; + } /* end switch */ + } /* end while */ + return (nchanges); +} /* end LoadOptimize */ + +/**********************************************************************/ +int StoreOptimize (void) +{ + uint32_t val; + int nchanges = 0; + register int i; + + TRACE(stderr, "[StoreOptimize]"); + + /* At least two pcodes are need to perform the following Store */ + /* optimizations */ + + i = 0; + while (i < nops-1) + { + switch (GETOP(pptr[i])) + { + /* Eliminate store followed by load */ + + case oSTSH : + if ((GETOP(pptr[i+1]) == oLDSH) && + (GETARG(pptr[i+1]) == GETARG(pptr[i]))) + { + PUTOP(pptr[i+1], oSTSH); + PUTOP(pptr[i], oDUP); + PUTARG(pptr[i], 0); + nchanges++; + i += 2; + } /* end if */ + else i++; + break; + + /* Convert stores indexed by a constant to unindexed stores */ + case oPUSH : + /* Get the index value */ + + val = (int32_t)GETARG(pptr[i]); + + /* 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 (GETOP(pptr[i+2]) == oSTSXH) + { + PUTOP(pptr[i+2], oSTSH); + val += GETARG(pptr[i+2]); + PUTARG(pptr[i+2], val); + deletePcode (i); + nchanges++; + } /* end if */ + else if (GETOP(pptr[i+2]) == oSTSXB) + { + PUTOP(pptr[i+2], oSTSB); + val += GETARG(pptr[i+2]); + PUTARG(pptr[i+2], val); + 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/insn32/popt/psopt.c b/misc/pascal/insn32/popt/psopt.c index 6794115c8..16824106e 100644 --- a/misc/pascal/insn32/popt/psopt.c +++ b/misc/pascal/insn32/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,7 +133,7 @@ static inline void flushc(int c, poffProgHandle_t poffProgHandle) * buffer */ - poffAddTmpProgByte(poffProgHandle, (ubyte)c); + poffAddTmpProgByte(poffProgHandle, (uint8_t)c); } } @@ -144,33 +145,33 @@ static inline void flushbuf(poffProgHandle_t poffProgHandle) 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,43 +189,43 @@ 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 a 32-bit argument */ + /* Check for a 32-bit argument */ - if ((opcode & o32) != 0) - { - /* Echo the 32-bits of the argument */ + if ((opcode & o32) != 0) + { + /* Echo the 32-bits of the 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); - } - } + 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--; + } } } @@ -244,97 +245,97 @@ 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) - { - uint32 arg32; - unsigned int tmp; + { + uint32_t arg32; + unsigned int tmp; - /* Get the 32-bit argument from the big endian data stream */ + /* Get the 32-bit argument from the big endian data stream */ - tmp = poffGetProgByte(poffHandle); - arg32 = tmp << 24; - putbuf(tmp, poffProgHandle); + tmp = poffGetProgByte(poffHandle); + arg32 = tmp << 24; + putbuf(tmp, poffProgHandle); - tmp = poffGetProgByte(poffHandle); - arg32 |= tmp << 16; - putbuf(tmp, poffProgHandle); + tmp = poffGetProgByte(poffHandle); + arg32 |= tmp << 16; + putbuf(tmp, poffProgHandle); - tmp = poffGetProgByte(poffHandle); - arg32 |= tmp << 8; - putbuf(tmp, poffProgHandle); + tmp = poffGetProgByte(poffHandle); + arg32 |= tmp << 8; + putbuf(tmp, poffProgHandle); - tmp = poffGetProgByte(poffHandle); - arg32 |= tmp; - putbuf(tmp, poffProgHandle); + tmp = poffGetProgByte(poffHandle); + arg32 |= tmp; + putbuf(tmp, poffProgHandle); - inch = poffGetProgByte(poffHandle); + inch = poffGetProgByte(poffHandle); - /* Is it LIB MKSTK? MKSTKSTR? or MKSTKC? */ + /* Is it LIB MKSTK? MKSTKSTR? or MKSTKC? */ - if ((arg32 == lbMKSTK) || - (arg32 == lbMKSTKSTR) || - (arg32 == lbMKSTKC)) - { - /* Flush the buffered data with the PUSHS */ + if ((arg32 == lbMKSTK) || + (arg32 == lbMKSTKSTR) || + (arg32 == lbMKSTKC)) + { + /* Flush the buffered data with the PUSHS */ - flushc(oPUSHS, poffProgHandle); - flushbuf(poffProgHandle); + flushc(oPUSHS, poffProgHandle); + flushbuf(poffProgHandle); - /* And break out of the loop to search for - * the next PUSHS - */ + /* And break out of the loop to search for + * the next PUSHS + */ - break; - } - } + break; + } + } else - { - int opcode; + { + int opcode; - /* 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 a 32-bit argument */ + /* Check for a 32-bit argument */ - if (opcode & o32 != 0) - { - /* Buffer the remaining 24-bits of the argument */ + if (opcode & o32 != 0) + { + /* Buffer the remaining 24-bits of the 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); - } - } + putbuf(inch, poffProgHandle); + inch = poffGetProgByte(poffHandle); + putbuf(inch, poffProgHandle); + inch = poffGetProgByte(poffHandle); + putbuf(inch, poffProgHandle); + inch = poffGetProgByte(poffHandle); + putbuf(inch, poffProgHandle); + inch = poffGetProgByte(poffHandle); + } + } } } @@ -343,7 +344,7 @@ static void dopop(poffHandle_t poffHandle, poffProgHandle_t poffProgHandle) **********************************************************************/ void stringStackOptimize(poffHandle_t poffHandle, - poffProgHandle_t poffProgHandle) + poffProgHandle_t poffProgHandle) { int i; @@ -351,12 +352,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; } |