summaryrefslogtreecommitdiff
path: root/misc/pascal/insn16/prun/prun.c
diff options
context:
space:
mode:
authorpatacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>2008-01-04 22:52:02 +0000
committerpatacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>2008-01-04 22:52:02 +0000
commiteca8fc5427fae36404c8f3772f8ed4f2f36100ea (patch)
tree15ba7e23c47bc4d4b4fdc59d730c3e0eb7b03fa6 /misc/pascal/insn16/prun/prun.c
parent2d2c6392898b29ae89d41a779ef712fb901900ac (diff)
downloadnuttx-eca8fc5427fae36404c8f3772f8ed4f2f36100ea.tar.gz
nuttx-eca8fc5427fae36404c8f3772f8ed4f2f36100ea.tar.bz2
nuttx-eca8fc5427fae36404c8f3772f8ed4f2f36100ea.zip
Pascal
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@485 42af7a65-404d-4744-a932-0658087f49c3
Diffstat (limited to 'misc/pascal/insn16/prun/prun.c')
-rw-r--r--misc/pascal/insn16/prun/prun.c257
1 files changed, 257 insertions, 0 deletions
diff --git a/misc/pascal/insn16/prun/prun.c b/misc/pascal/insn16/prun/prun.c
new file mode 100644
index 000000000..cc7c91111
--- /dev/null
+++ b/misc/pascal/insn16/prun/prun.c
@@ -0,0 +1,257 @@
+/****************************************************************************
+ * prun.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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <getopt.h>
+#include <string.h>
+#include <errno.h>
+#include <ctype.h>
+
+#include "keywords.h"
+#include "pdefs.h"
+#include "pinsn16.h"
+#include "pxdefs.h"
+#include "pedefs.h"
+#include "pmach.h"
+
+#include "paslib.h"
+#include "perr.h"
+#include "pexec.h"
+#include "pdbg.h"
+
+/****************************************************************************
+ * Definitions
+ ****************************************************************************/
+
+#define MIN_STACK_SIZE 1024
+#define DEFAULT_STACK_SIZE 4096
+#define DEFAULT_STKSTR_SIZE 0
+
+/****************************************************************************
+ * Private Type Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Constant Data
+ ****************************************************************************/
+
+static const struct option long_options[] =
+{
+ {"stack", 1, NULL, 's'},
+ {"string", 1, NULL, 't'},
+ {"debug", 0, NULL, 'd'},
+ {"help", 0, NULL, 'h'},
+ {NULL, 0, NULL, 0}
+};
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const char *poffFileName;
+static sint32 varStackSize = DEFAULT_STACK_SIZE;
+static sint32 stringStackSize = DEFAULT_STKSTR_SIZE;
+static int debug = 0;
+
+/****************************************************************************
+ * Global Variables
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void showUsage(const char *progname)
+{
+ fprintf(stderr, "Usage:\n");
+ fprintf(stderr, " %s [options] <program-filename>\n",
+ progname);
+ fprintf(stderr, "options:\n");
+ fprintf(stderr, " -s <stack-size>\n");
+ fprintf(stderr, " --stack <stack-size>\n");
+ fprintf(stderr, " Memory in bytes to allocate for the pascal program\n");
+ fprintf(stderr, " stack in bytes (minimum is %d; default is %d bytes)\n",
+ MIN_STACK_SIZE, DEFAULT_STACK_SIZE);
+ fprintf(stderr, " -t <stack-size>\n");
+ fprintf(stderr, " --string <string-storage-size>\n");
+ fprintf(stderr, " Memory in bytes to allocate for the pascal program\n");
+ fprintf(stderr, " string storage in bytes (default is %d bytes)\n",
+ DEFAULT_STKSTR_SIZE);
+ fprintf(stderr, " -d\n");
+ fprintf(stderr, " --debug\n");
+ fprintf(stderr, " Enable PCode program debugger\n");
+ fprintf(stderr, " -h\n");
+ fprintf(stderr, " --help\n");
+ fprintf(stderr, " Shows this message\n");
+ exit(1);
+}
+
+static void parse_args(int argc, char **argv)
+{
+ int option_index;
+ int size;
+ int c;
+
+ /* Check for existence of filename argument */
+
+ if (argc < 2)
+ {
+ fprintf(stderr, "ERROR: Filename required\n");
+ showUsage(argv[0]);
+ } /* end if */
+
+ /* Parse the command line options */
+
+ do
+ {
+ c = getopt_long (argc, argv, "t:s:dh",
+ long_options, &option_index);
+ if (c != -1)
+ {
+ switch (c)
+ {
+ case 's' :
+ size = atoi(optarg);
+ if (size < MIN_STACK_SIZE)
+ {
+ fprintf(stderr, "ERROR: Invalid stack size\n");
+ showUsage(argv[0]);
+ }
+ varStackSize = (size + 3) & ~3;
+ break;
+
+ case 't' :
+ size = atoi(optarg);
+ if (size < 0)
+ {
+ fprintf(stderr, "ERROR: Invalid string storage size\n");
+ showUsage(argv[0]);
+ }
+ stringStackSize = ((size + 3) & ~3);
+ break;
+
+ case 'd' :
+ debug++;
+ break;
+
+ case 'h' :
+ showUsage(argv[0]);
+ break;
+
+ default:
+ /* Shouldn't happen */
+
+ fprintf(stderr, "ERROR: Unrecognized option\n");
+ showUsage(argv[0]);
+ }
+ }
+ }
+ while (c != -1);
+
+ if (optind != argc-1)
+ {
+ fprintf(stderr, "ERROR: Only one filename permitted on command line\n");
+ showUsage(argv[0]);
+ }
+
+ /* Get the name of the p-code file(s) from the last argument(s) */
+
+ poffFileName = argv[argc-1];
+}
+
+/* This function executes the P-Code program until a stopping condition
+ * is encountered. */
+
+static void run(struct pexec_s *st)
+{
+ uint16 errcode;
+
+ for (;;)
+ {
+ /* Execute the instruction; Check for exceptional conditions */
+
+ errcode = pexec(st);
+ if (errcode != eNOERROR) break;
+ }
+
+ if (errcode != eEXIT)
+ {
+ printf("Runtime error 0x%02x -- Execution Stopped\n", errcode);
+ }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+int main(int argc, char *argv[], char *envp[])
+{
+ struct pexec_s *st;
+ char fileName[FNAME_SIZE+1]; /* Object file name */
+
+ /* Parse the command line arguments */
+
+ parse_args(argc, argv);
+
+ /* Load the POFF files specified on the command line */
+ /* Use .o or command line extension, if supplied */
+
+ (void)extension(poffFileName, "o", fileName, 0);
+
+ /* Load the POFF file */
+
+ st = pload(fileName, varStackSize, stringStackSize);
+ if (!st)
+ {
+ fprintf(stderr, "ERROR: Could not load %s\n", fileName);
+ exit(1);
+ } /* end if */
+ printf("%s Loaded\n", fileName);
+
+ /* And start program execution in the specified mode */
+
+ if (debug)
+ dbg_run(st);
+ else
+ run(st);
+
+ return 0;
+
+} /* end main */