File: crasm.c

package info (click to toggle)
crasm 1.8-1
  • links: PTS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch
  • size: 376 kB
  • sloc: ansic: 3,505; makefile: 65
file content (488 lines) | stat: -rwxr-xr-x 8,846 bytes parent folder | download | duplicates (2)
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/*  Copyright (C) 1987- Leon Bottou
 * 
 *  This is free documentation; 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 of
 *  the License, or (at your option) any later version.
 * 
 *  The GNU General Public License's references to "object code"
 *  and "executables" are to be interpreted as the output of any
 *  document formatting or typesetting system, including
 *  intermediate and printed output.
 * 
 *  This manual 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 this manual. Otherwise check the web site
 *  of the Free Software Foundation at http://www.fsf.org.
 */
/*
	CRASM: crasm.c
	noyau central
	LYB 9/87
*/

#include "dcl.h"
#include "version.h"
#include "label.h"

#define SCODE 1
#define LIST  2
#define XREF  4
#define WARN  8
#define INPUT 128


extern struct label *searchlabel();
extern struct label pseudos[];

jmp_buf errorjump;
int errnumber,warnnumber;
int segment;
extern int includelevel, thiscall;

void 
jmp_buf_copy(jmp_buf out, jmp_buf in)
{
#ifdef JBUFSIZE
  int i;
  for (i=0; i<JBUFSIZE; i++)
    out[i] = in[i];
#else
  memcpy(out, in, sizeof(jmp_buf));
#endif
}


int
main(int argc, char **argv)
{
	int flags;
	char *s;

	passnumber = 0;
	flags= LIST | WARN;
#ifdef DEBUG
	flags |= XREF;
#endif
	if (argc==0)
		fatal("NOT from WB");
	while ( --argc )
	{	if ( **(++argv)=='-' )
		{	for ( s=(*argv)+1; *s; s++ )
				switch ( *s)
				{
				case 's':	/* suppress warnings */
					flags &= ~WARN;
					break;
				case 'l':	/* suppress listing  */
					flags &= ~LIST;
					break;
				case 'x':	/* force XREF	     */
					flags |= XREF;
					break;
				case 'o':	/* output name	     */
					if (!s[1] && argc>1 && !(flags&SCODE) )
					{	flags |= SCODE;
						scodename=*++argv;
						--argc;
					}
					break;
				default:
					syntax("Bad Option(s)");
				}
		}
		else
		{	if ( flags & INPUT )
				syntax("One input only");
			filename=*argv;
			flags |= INPUT;
		};
	}
	if ( flags & INPUT )
                crasm(flags);
	else
		syntax("No input!");
	return 0;
}


void
crasm(int flag)
{
	init_label_list(pseudos);
	file=fopen(filename,"r");
	if ( file==NULL )
		fatal("can't open source file");

	scode = NULL;
	llen=76; plen=66; ppos=0 ; lpos=0;
	
#ifdef DEBUG
	asmflags = F_NOCODE;
#else
	asmflags = F_NOLIST | F_NOWARNING| F_NOERROR | F_NOCODE;
#endif

	pass(1);

#ifdef DEBUG
	outputEOP();
	asmflags &= ~ (F_NOLIST | F_NOWARNING | F_NOCODE | F_NOERROR );
#else
	asmflags &= ~F_NOERROR;
	if (flag & SCODE)
		asmflags &= ~F_NOCODE;
	if (flag & LIST)
		asmflags &= ~F_NOLIST;
	if (flag & WARN)
		asmflags &= ~F_NOWARNING;
#endif
	

	
	undeflabels(lroot);
	pass(2);

	if ( scode )
		closescodefile();
	
	asmflags  |= F_LIST_ON;

	if (errnumber==0 && flag & LIST)
		flag |= XREF;

	result();
	if (errnumber && (flag & SCODE) )
		remove(scodename);
		
	if (flag & XREF)
		xref();
}



void 
pass(int n)
{
	clearerr(file);
	rewind(file);

	asmflags |= F_CODE_ON;
	if ( ! (asmflags & F_NOLIST) )
		asmflags |= F_LIST_ON;
#ifdef DEBUG
	asmflags |= F_IFLIST_ON | F_MACROLIST_ON;
#else
	asmflags &= ~ F_MACROLIST_ON;
	asmflags |=   F_IFLIST_ON;
#endif	
	macrolevel	=0;
	thiscall	=0;
	segment		=0;
	includelevel	=0;
	linenumber	=0;
	lineprefix	=0;
	
	starlabel = findlabel("*");
	starlabel->flags |= UNDEF;
	starlabel->flags &= ~NOREDEF;
	
	
	passnumber=n;
        fprintf(stderr,"Pass #%d\n",n);
        
	errnumber=warnnumber=0;
	
	while ( !feof(file) )
	{	if (setjmp(errorjump) )
			errnumber++;
                linegets(curline,250);
		if (asmline(curline,3))
			error("ELSE, ENDC, ENDM, EXITM illegal here");
	}
}


static void
herelabel(char *label)
{
	if ( strcmp(label,"*") == 0 )
		error ( "illegal star definition");
	if ( asmflags & F_ORG_GV )
		deflabel(label,NOREDEF,
			 asmflags & F_RELATIF ? L_RELATIVE : L_ABSOLUTE,
			 starlabel->value );
	else
		error ( "undefinable label: no org given" );
	
	if ( !(asmflags & F_CODE_ON) )
		if ( macrolevel==0 || asmflags & F_MACROLIST_ON )
	 		if ( lpos==0 )
			{	position(2);
				outputaddr(starlabel->value);
			};

}

static struct label *
findmnemo(char *mnemo)
{
	char *aux1,*aux2;
	register struct label *labmnemo;
	
	labmnemo = searchlabel(mnemo);
	if ( labmnemo == NULL )		/* sizer 68k */
	{	if ( filter(mnemo,"?.?",&aux1,&aux2) )
			if (( labmnemo = searchlabel(aux1) ))
				if (labmnemo->modifier >=0)
					labmnemo = NULL;
	};
	if ( labmnemo )
		if ( !(labmnemo->flags & UNDEF ) )
			if ( labmnemo->type == L_MNEMO ||
				labmnemo->type == L_MACRO )
					return labmnemo;
	return NULL;
}


static int 
nospacein(char *s)
{
 	while( *s )
	{	if ( *s == ' ' || *s == '\t' )
			return FALSE;
		s++;
	}
	return TRUE;
}


int
asmline(char *s, int status)
{
	char *label,*mnemo,*oper;
	register struct label *labmnemo;
	
	advance    = 0;
	if (starlabel->flags & UNDEF)
		asmflags &= ~F_ORG_GV;
	else
	{	asmflags |= F_ORG_GV;
		setpc ( starlabel->value );
	};
	
	zerobuffer();

/* on traite
  XXX ; REMARK
   puis les cas
  XXX = blanc
*/
	if ( filter( s,"_?_;?",&label,&mnemo ) )
		s=label;
	else
	{ 	register char *p;
	  	p=s;
	  	while(*p) p++;
	  	while ( p>s && (*--p==' ' || *p=='\t') ) *p=0;
	  	while (*s==' ' || *s=='\t') s++;
	}
	
	if (*s==0)
	{	if (status & 1)
			outputline();
		return 0;
	}

/*   autorise:
    LABEL= MNEMO
	MNEMO
  LABEL MNEMO
  	MNEMO OPERAND
  LABEL MNEMO OPERAND
*/

	if (filter ( s, "?_=_?", &label,&oper ) && nospacein(label) )
	{	mnemo="equ";
		labmnemo=findmnemo(mnemo);
		if (checklabel(label) == FALSE )
			error("malformed label");
	}
	else
	{	label = mnemo = oper = NULL;	
	start:	if (filter(s,"? _?",&mnemo,&oper) && *mnemo && *oper)
		{	labmnemo = findmnemo(mnemo);
			if ( labmnemo==NULL && label==NULL )
			{	label=mnemo;
				if (checklabel(label) == FALSE )
					error("malformed label");
				s=oper;
				goto start;
			}
		}
		else
		{	labmnemo=findmnemo(mnemo=s);
			oper = NULL;
		}
	}
	
	if ( labmnemo==NULL )
		error( "unknown macro or mnemonic");		
	if ( labmnemo->flags & NOLABEL )
		if (label)
			error("labels not allowed here");
	if ( labmnemo->flags & DEFLABEL )
	{	if (!label)
			error("label required here");
	}
	else if (label)
	{	if (status & 2)
			herelabel(label);			
	}

	if ( labmnemo->flags & DEFCOND )
		return	(*labmnemo->ptr)(	status,
						label,mnemo,oper    );
			
	if ( labmnemo->flags & DEFMACRO )
		return	(*labmnemo->ptr)(	status,
						label,mnemo,oper    );

	if ( labmnemo->type == L_MACRO )
	{	if (!label && oper && !strncmp(oper,"MACRO",5))
			error("macro is already defined");
		if (status & 2)
			macrocall( labmnemo,status,oper );
		else if (status & 1)
			outputline();
		return 0;
	}
	else if ( labmnemo->type == L_MNEMO )
	{	if (status & 2)
			(*labmnemo->ptr)(	labmnemo->modifier,
						label,mnemo,oper    );
		starlabel->value += advance;
		if ( status & 1 )
			outputline();
		return 0;
	}
	else
		fatal ("proc asmline failure");
	return 0;
}


/*   F_LIST_ON est pris en compte dans outputline()
     F_CODE_ON est pris en compte dans insertbyte()
     F_NOLIST  est pris en compte dans LIST ON
     F_NOCODE  est pris en compte dans CODE ON ou OFF
     
     F_NOERROR   dans  error
     F_NOWARNING dans  warning
*/



/*
	ERRORS ROUTINES
*/

void
error(char *s)
{
	char raw[80];
	int oldflags;
	char c = toupper(*s);
	
	oldflags=asmflags;
	
	if ( !(asmflags & F_NOERROR) )
	{	sprintf(raw,"%7d ERROR: %c%s",linenumber,c,s+1);
		for ( s=raw; s[1]==' '; s++ )
			s[0]='>';
		asmflags |= F_LIST_ON;
		outputraw(raw);
	}
	outputline();
	
	if ( advance != 0 )
		starlabel->value += advance;
	
	asmflags = oldflags;
	longjmp( errorjump, -1 );
}

void
warning(char *s)
{
	char raw[80];
	char c = toupper(*s);
	if ( !(asmflags & F_NOWARNING) )
	{	sprintf(raw,"%7d WARNING: %c%s",linenumber,c,s+1);
		for ( s=raw; s[1]==' '; s++ )
			s[0]='>';
		outputraw(raw);
	}
}


void 
fatal(char *s)
{
	printf("FATAL ERROR: %s\n",s);
	exit(10);
}


void
fileerror(char *s)
{
  printf("FILE ERROR on %s file: %s\n", s, strerror(errno));
}
	


/*  aff CPU known  */

extern struct cpulist {  char *name;
		  int code;
		  int  (*init)(); } cpulist[];


void
syntax(char *s)
{
	register struct cpulist *q;
	register int(*a)();
	
	a=NULL;
	printf ("%s\n",s);
	printf ("Syntax:  crasm [-slx] [-o SCODEFILE] INPUTFILE\n");
 	printf ("Crasm %s known CPUs:",CRASMVERSION);
	for ( q=cpulist; q->name ; q++ )
	{	if (a!=q->init)
			printf("\n\t");
		printf(" %s",q->name);
		a=q->init;
	}
	printf("\n");
	exit(10);
}


void
setflag(int f)
{
	asmflags |= f;
}

void
clrflag(int f)
{
	asmflags &= ~f;
}