File: main.c

package info (click to toggle)
gpasm 0.0.7-5
  • links: PTS
  • area: main
  • in suites: potato
  • size: 536 kB
  • ctags: 310
  • sloc: ansic: 2,011; perl: 1,330; yacc: 368; lex: 331; sh: 152; makefile: 34
file content (161 lines) | stat: -rw-r--r-- 3,879 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/* Main function for gpasm
   Copyright (C) 1998 James Bowman

This file is part of gpasm.

gpasm is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

gpasm is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with gpasm; see the file COPYING.  If not, write to
the Free Software Foundation, 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.  */

#include "stdhdr.h"

#include "gpasm.h"
#include "symbol.h"
#include "opcode.h"
#include "lst.h"
#include "hex.h"

struct gpasm_state state = {
    16,		/* radix */
    inhx8m,	/* hex_format */
    1,		/* case_insensitive */
    0		/* quiet */
};

int yyparse(void);

int main( int argc, char *argv[] )
{
  int c;
  int usage = 0;

  /* #defines are case-insensitive */
  state.stDefines = push_symbol_table(NULL, 1);

  while ((c = getopt(argc, argv, "?a:cd:e:hp:qr:w:v")) != EOF) {
    switch (c) {
    case '?':
    case 'h':
      usage = 1;
      break;
    case 'a':
      if (strcasecmp(optarg, "inhx8m") == 0)
	state.hex_format = inhx8m;
      else if (strcasecmp(optarg, "inhx8s") == 0)
	state.hex_format = inhx8s;
      else if (strcasecmp(optarg, "inhx32") == 0)
	state.hex_format = inhx32;
      else {
	fprintf(stderr,
		"Error: unrecognised hex file format \"%s\"\n",
		optarg);
      }
      break;
    case 'c':
      state.case_insensitive = 0;
      break;
    case 'd':
      if ((optarg != NULL) && (strlen(optarg) > 0)) {
	struct symbol *sym;
	char *lhs, *rhs;

	lhs = strdup(optarg);
	rhs = strchr(lhs, '=');
	if (rhs != NULL) {
	  *rhs = '\0';	/* Terminate the left-hand side */
	  rhs++;	/* right-hand side begins after the '=' */
	}

	sym = get_symbol(state.stDefines, lhs);
	if (sym == NULL)
	  sym = add_symbol(state.stDefines, lhs);
	if (rhs)
	  annotate_symbol(sym, rhs);
      }
      break;
    case 'p':
      select_processor(optarg);
      break;
    case 'q':
      state.quiet = 1;
      break;
    case 'r':
      select_radix(optarg);
      break;
    case 'v':
      fprintf(stderr, "%s\n", GPASM_VERSION_STRING);
      exit(0);
      
    }
    if (usage)
      break;
  }
  
  if (optind < argc)
    state.srcfilename = argv[optind];
  else
    usage = 1;

  if (usage) {
      printf("\n");
      printf("usage: gpasm <options> <filename>\n");
      printf("Where <options> are:\n");
      printf(" -a <fmt>       Select hex format inhx8m, inhx8s, inhx32\n");
      printf(" -c             case sensitive \n");
      printf(" -d <sym>=<val> Define symbol with value\n");
      printf(" -h             Show this usage message \n");
      printf(" -p <processor> Select processor\n");
      printf(" -q             Quiet \n");
      printf(" -r <radix>     Select radix \n");
      printf(" -v             Show version \n");
      printf("\n");
      exit(0);
  }

  /* Builtins and macros are always case insensitive */
  state.stBuiltin = push_symbol_table(NULL, 1);
  state.stMacros = push_symbol_table(NULL, 1);
  state.stTop = 
    state.stGlobal = push_symbol_table(NULL, state.case_insensitive);

  opcode_init(0);

  state.maxram = (MAX_RAM - 1);

  open_file(SPECIAL_PATH);

  state.pass = 1;

  yyparse();

  assert(state.pass == 2);
  
  pop_symbol_table(state.stBuiltin);
  
  dump_hex();

  /* Maybe produce a symbol table */
  if (state.lst.symboltable) {
    lst_throw(); /* Start symbol table on a fresh page */
    dump_symbol_table(state.stGlobal);
  }

  /* Finish off the listing */
  lst_close();

  if (state.num.errors > 0)
    return 1;
  else
    return 0;
}