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