xref: /OK3568_Linux_fs/kernel/drivers/scsi/aic7xxx/aicasm/aicasm_macro_gram.y (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun %{
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Sub-parser for macro invocation in the Aic7xxx SCSI
4*4882a593Smuzhiyun  * Host adapter sequencer assembler.
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * Copyright (c) 2001 Adaptec Inc.
7*4882a593Smuzhiyun  * All rights reserved.
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * Redistribution and use in source and binary forms, with or without
10*4882a593Smuzhiyun  * modification, are permitted provided that the following conditions
11*4882a593Smuzhiyun  * are met:
12*4882a593Smuzhiyun  * 1. Redistributions of source code must retain the above copyright
13*4882a593Smuzhiyun  *    notice, this list of conditions, and the following disclaimer,
14*4882a593Smuzhiyun  *    without modification.
15*4882a593Smuzhiyun  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
16*4882a593Smuzhiyun  *    substantially similar to the "NO WARRANTY" disclaimer below
17*4882a593Smuzhiyun  *    ("Disclaimer") and any redistribution must be conditioned upon
18*4882a593Smuzhiyun  *    including a substantially similar Disclaimer requirement for further
19*4882a593Smuzhiyun  *    binary redistribution.
20*4882a593Smuzhiyun  * 3. Neither the names of the above-listed copyright holders nor the names
21*4882a593Smuzhiyun  *    of any contributors may be used to endorse or promote products derived
22*4882a593Smuzhiyun  *    from this software without specific prior written permission.
23*4882a593Smuzhiyun  *
24*4882a593Smuzhiyun  * Alternatively, this software may be distributed under the terms of the
25*4882a593Smuzhiyun  * GNU General Public License ("GPL") version 2 as published by the Free
26*4882a593Smuzhiyun  * Software Foundation.
27*4882a593Smuzhiyun  *
28*4882a593Smuzhiyun  * NO WARRANTY
29*4882a593Smuzhiyun  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30*4882a593Smuzhiyun  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31*4882a593Smuzhiyun  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
32*4882a593Smuzhiyun  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33*4882a593Smuzhiyun  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34*4882a593Smuzhiyun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35*4882a593Smuzhiyun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36*4882a593Smuzhiyun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37*4882a593Smuzhiyun  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
38*4882a593Smuzhiyun  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39*4882a593Smuzhiyun  * POSSIBILITY OF SUCH DAMAGES.
40*4882a593Smuzhiyun  *
41*4882a593Smuzhiyun  * $Id: //depot/aic7xxx/aic7xxx/aicasm/aicasm_macro_gram.y#5 $
42*4882a593Smuzhiyun  *
43*4882a593Smuzhiyun  * $FreeBSD$
44*4882a593Smuzhiyun  */
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun #include <sys/types.h>
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun #include <inttypes.h>
49*4882a593Smuzhiyun #include <regex.h>
50*4882a593Smuzhiyun #include <stdio.h>
51*4882a593Smuzhiyun #include <stdlib.h>
52*4882a593Smuzhiyun #include <string.h>
53*4882a593Smuzhiyun #include <sysexits.h>
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun #include "../queue.h"
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun #include "aicasm.h"
58*4882a593Smuzhiyun #include "aicasm_symbol.h"
59*4882a593Smuzhiyun #include "aicasm_insformat.h"
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun static symbol_t *macro_symbol;
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun static void add_macro_arg(const char *argtext, int position);
64*4882a593Smuzhiyun void mmerror(const char *string);
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun %}
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun %union {
69*4882a593Smuzhiyun 	int		value;
70*4882a593Smuzhiyun 	char		*str;
71*4882a593Smuzhiyun 	symbol_t	*sym;
72*4882a593Smuzhiyun }
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun %token <str> T_ARG
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun %token <sym> T_SYMBOL
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun %type <value> macro_arglist
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun %%
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun macrocall:
84*4882a593Smuzhiyun 	T_SYMBOL '('
85*4882a593Smuzhiyun 	{
86*4882a593Smuzhiyun 		macro_symbol = $1;
87*4882a593Smuzhiyun 	}
88*4882a593Smuzhiyun 	macro_arglist ')'
89*4882a593Smuzhiyun 	{
90*4882a593Smuzhiyun 		if (macro_symbol->info.macroinfo->narg != $4) {
91*4882a593Smuzhiyun 			printf("Narg == %d", macro_symbol->info.macroinfo->narg);
92*4882a593Smuzhiyun 			stop("Too few arguments for macro invocation",
93*4882a593Smuzhiyun 			     EX_DATAERR);
94*4882a593Smuzhiyun 			/* NOTREACHED */
95*4882a593Smuzhiyun 		}
96*4882a593Smuzhiyun 		macro_symbol = NULL;
97*4882a593Smuzhiyun 		YYACCEPT;
98*4882a593Smuzhiyun 	}
99*4882a593Smuzhiyun ;
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun macro_arglist:
102*4882a593Smuzhiyun 	{
103*4882a593Smuzhiyun 		/* Macros can take 0 arguments */
104*4882a593Smuzhiyun 		$$ = 0;
105*4882a593Smuzhiyun 	}
106*4882a593Smuzhiyun |	T_ARG
107*4882a593Smuzhiyun 	{
108*4882a593Smuzhiyun 		$$ = 1;
109*4882a593Smuzhiyun 		add_macro_arg($1, 1);
110*4882a593Smuzhiyun 	}
111*4882a593Smuzhiyun |	macro_arglist ',' T_ARG
112*4882a593Smuzhiyun 	{
113*4882a593Smuzhiyun 		if ($1 == 0) {
114*4882a593Smuzhiyun 			stop("Comma without preceding argument in arg list",
115*4882a593Smuzhiyun 			     EX_DATAERR);
116*4882a593Smuzhiyun 			/* NOTREACHED */
117*4882a593Smuzhiyun 		}
118*4882a593Smuzhiyun 		$$ = $1 + 1;
119*4882a593Smuzhiyun 		add_macro_arg($3, $$);
120*4882a593Smuzhiyun 	}
121*4882a593Smuzhiyun ;
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun %%
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun static void
126*4882a593Smuzhiyun add_macro_arg(const char *argtext, int argnum)
127*4882a593Smuzhiyun {
128*4882a593Smuzhiyun 	struct macro_arg *marg;
129*4882a593Smuzhiyun 	int i;
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun 	if (macro_symbol == NULL || macro_symbol->type != MACRO) {
132*4882a593Smuzhiyun 		stop("Invalid current symbol for adding macro arg",
133*4882a593Smuzhiyun 		     EX_SOFTWARE);
134*4882a593Smuzhiyun 		/* NOTREACHED */
135*4882a593Smuzhiyun 	}
136*4882a593Smuzhiyun 	/*
137*4882a593Smuzhiyun 	 * Macro Invocation.  Find the appropriate argument and fill
138*4882a593Smuzhiyun 	 * in the replace ment text for this call.
139*4882a593Smuzhiyun 	 */
140*4882a593Smuzhiyun 	i = 0;
141*4882a593Smuzhiyun 	STAILQ_FOREACH(marg, &macro_symbol->info.macroinfo->args, links) {
142*4882a593Smuzhiyun 		i++;
143*4882a593Smuzhiyun 		if (i == argnum)
144*4882a593Smuzhiyun 			break;
145*4882a593Smuzhiyun 	}
146*4882a593Smuzhiyun 	if (marg == NULL) {
147*4882a593Smuzhiyun 		stop("Too many arguments for macro invocation", EX_DATAERR);
148*4882a593Smuzhiyun 		/* NOTREACHED */
149*4882a593Smuzhiyun 	}
150*4882a593Smuzhiyun 	marg->replacement_text = strdup(argtext);
151*4882a593Smuzhiyun 	if (marg->replacement_text == NULL) {
152*4882a593Smuzhiyun 		stop("Unable to replicate replacement text", EX_SOFTWARE);
153*4882a593Smuzhiyun 		/* NOTREACHED */
154*4882a593Smuzhiyun 	}
155*4882a593Smuzhiyun }
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun void
mmerror(const char * string)158*4882a593Smuzhiyun mmerror(const char *string)
159*4882a593Smuzhiyun {
160*4882a593Smuzhiyun 	stop(string, EX_DATAERR);
161*4882a593Smuzhiyun }
162