1 /*****************************************************************************/
5 /* Inline assembler statements for the cc65 C compiler */
9 /* (C) 2001-2008 Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@musoftware.de */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
56 /*****************************************************************************/
58 /*****************************************************************************/
62 static void AsmRangeError (unsigned Arg)
63 /* Print a diagnostic about a range error in the argument with the given number */
65 Error ("Range error in argument %u", Arg);
70 static void AsmErrorSkip (void)
71 /* Called in case of an error, skips tokens until the closing paren or a
72 ** semicolon is reached.
75 static const token_t TokenList[] = { TOK_RPAREN, TOK_SEMI };
76 SkipTokens (TokenList, sizeof(TokenList) / sizeof(TokenList[0]));
81 static SymEntry* AsmGetSym (unsigned Arg, unsigned Type)
82 /* Find the symbol with the name currently in NextTok. The symbol must be of
83 ** the given type. On errors, NULL is returned.
88 /* We expect an argument separated by a comma */
91 /* Argument must be an identifier */
92 if (CurTok.Tok != TOK_IDENT) {
93 Error ("Identifier expected for argument %u", Arg);
98 /* Get a pointer to the symbol table entry */
99 Sym = FindSym (CurTok.Ident);
101 /* Did we find a symbol with this name? */
103 Error ("Undefined symbol `%s' for argument %u", CurTok.Ident, Arg);
108 /* We found the symbol - skip the name token */
111 /* Check if we have a global symbol */
112 if ((Sym->Flags & Type) != Type) {
113 Error ("Type of argument %u differs from format specifier", Arg);
118 /* Mark the symbol as referenced */
119 Sym->Flags |= SC_REF;
127 static void ParseByteArg (StrBuf* T, unsigned Arg)
128 /* Parse the %b format specifier */
133 /* We expect an argument separated by a comma */
136 /* Evaluate the expression */
137 ConstAbsIntExpr (hie1, &Expr);
139 /* Check the range but allow negative values if the type is signed */
140 if (IsSignUnsigned (Expr.Type)) {
141 if (Expr.IVal < 0 || Expr.IVal > 0xFF) {
146 if (Expr.IVal < -128 || Expr.IVal > 127) {
152 /* Convert into a hex number */
153 xsprintf (Buf, sizeof (Buf), "$%02lX", Expr.IVal & 0xFF);
155 /* Add the number to the target buffer */
156 SB_AppendStr (T, Buf);
161 static void ParseWordArg (StrBuf* T, unsigned Arg)
162 /* Parse the %w format specifier */
167 /* We expect an argument separated by a comma */
170 /* Evaluate the expression */
171 ConstAbsIntExpr (hie1, &Expr);
173 /* Check the range but allow negative values if the type is signed */
174 if (IsSignUnsigned (Expr.Type)) {
175 if (Expr.IVal < 0 || Expr.IVal > 0xFFFF) {
180 if (Expr.IVal < -32768 || Expr.IVal > 32767) {
186 /* Convert into a hex number */
187 xsprintf (Buf, sizeof (Buf), "$%04lX", Expr.IVal & 0xFFFF);
189 /* Add the number to the target buffer */
190 SB_AppendStr (T, Buf);
195 static void ParseLongArg (StrBuf* T, unsigned Arg attribute ((unused)))
196 /* Parse the %l format specifier */
201 /* We expect an argument separated by a comma */
204 /* Evaluate the expression */
205 ConstAbsIntExpr (hie1, &Expr);
207 /* Convert into a hex number */
208 xsprintf (Buf, sizeof (Buf), "$%08lX", Expr.IVal & 0xFFFFFFFF);
210 /* Add the number to the target buffer */
211 SB_AppendStr (T, Buf);
216 static void ParseGVarArg (StrBuf* T, unsigned Arg)
217 /* Parse the %v format specifier */
219 /* Parse the symbol name parameter and check the type */
220 SymEntry* Sym = AsmGetSym (Arg, SC_STATIC);
222 /* Some sort of error */
226 /* Check for external linkage */
227 if (Sym->Flags & (SC_EXTERN | SC_STORAGE | SC_FUNC)) {
228 /* External linkage or a function */
229 /* ### FIXME: Asm name should be generated by codegen */
230 SB_AppendChar (T, '_');
231 SB_AppendStr (T, Sym->Name);
232 } else if (Sym->Flags & SC_REGISTER) {
234 xsprintf (Buf, sizeof (Buf), "regbank+%d", Sym->V.R.RegOffs);
235 SB_AppendStr (T, Buf);
237 /* Static variable */
239 xsprintf (Buf, sizeof (Buf), "L%04X", Sym->V.Label);
240 SB_AppendStr (T, Buf);
246 static void ParseLVarArg (StrBuf* T, unsigned Arg)
247 /* Parse the %o format specifier */
252 /* Parse the symbol name parameter and check the type */
253 SymEntry* Sym = AsmGetSym (Arg, SC_AUTO);
255 /* Some sort of error */
259 /* The symbol may be a parameter to a variadic function. In this case, we
260 ** don't have a fixed stack offset, so check it and bail out with an error
261 ** if this is the case.
263 if ((Sym->Flags & SC_PARAM) == SC_PARAM && F_IsVariadic (CurrentFunc)) {
264 Error ("Argument %u has no fixed stack offset", Arg);
269 /* Calculate the current offset from SP */
270 Offs = Sym->V.Offs - StackPtr;
272 /* Output the offset */
273 xsprintf (Buf, sizeof (Buf), (Offs > 0xFF)? "$%04X" : "$%02X", Offs);
274 SB_AppendStr (T, Buf);
279 static void ParseLabelArg (StrBuf* T, unsigned Arg attribute ((unused)))
280 /* Parse the %g format specifier */
282 /* We expect an identifier separated by a comma */
284 if (CurTok.Tok != TOK_IDENT) {
286 Error ("Label name expected");
290 /* Add a new label symbol if we don't have one until now */
291 SymEntry* Entry = AddLabelSym (CurTok.Ident, SC_REF);
293 /* Append the label name to the buffer */
294 SB_AppendStr (T, LocalLabelName (Entry->V.Label));
296 /* Eat the label name */
304 static void ParseStrArg (StrBuf* T, unsigned Arg attribute ((unused)))
305 /* Parse the %s format specifier */
310 /* We expect an argument separated by a comma */
313 /* Check what comes */
314 switch (CurTok.Tok) {
318 SB_AppendStr (T, CurTok.Ident);
323 /* String constant */
324 SB_Append (T, GetLiteralStrBuf (CurTok.SVal));
329 ConstAbsIntExpr (hie1, &Expr);
330 xsprintf (Buf, sizeof (Buf), "%ld", Expr.IVal);
331 SB_AppendStr (T, Buf);
338 static void ParseAsm (void)
339 /* Parse the contents of the ASM statement */
344 /* Create a target string buffer */
345 StrBuf T = AUTO_STRBUF_INITIALIZER;
347 /* Create a string buffer from the string literal */
348 StrBuf S = AUTO_STRBUF_INITIALIZER;
349 SB_Append (&S, GetLiteralStrBuf (CurTok.SVal));
351 /* Skip the string token */
354 /* Parse the statement. It may contain several lines and one or more
355 ** of the following place holders:
356 ** %b - Numerical 8 bit value
357 ** %w - Numerical 16 bit value
358 ** %l - Numerical 32 bit value
359 ** %v - Assembler name of a (global) variable
360 ** %o - Stack offset of a (local) variable
361 ** %g - Assembler name of a C label
362 ** %s - Any argument converted to a string (almost)
366 while ((C = SB_Get (&S)) != '\0') {
368 /* If it is a newline, the current line is ready to go */
371 /* Pass it to the backend and start over */
375 } else if (C == '%') {
377 /* Format specifier */
381 case '%': SB_AppendChar (&T, '%'); break;
382 case 'b': ParseByteArg (&T, Arg); break;
383 case 'g': ParseLabelArg (&T, Arg); break;
384 case 'l': ParseLongArg (&T, Arg); break;
385 case 'o': ParseLVarArg (&T, Arg); break;
386 case 's': ParseStrArg (&T, Arg); break;
387 case 'v': ParseGVarArg (&T, Arg); break;
388 case 'w': ParseWordArg (&T, Arg); break;
390 Error ("Error in __asm__ format specifier %u", Arg);
397 /* A normal character, just copy it */
398 SB_AppendChar (&T, C);
403 /* If the target buffer is not empty, we have a last line in there */
404 if (!SB_IsEmpty (&T)) {
409 /* Call the string buf destructors */
416 void AsmStatement (void)
417 /* This function parses ASM statements. The syntax of the ASM directive
418 ** looks like the one defined for C++ (C has no ASM directive), that is,
419 ** a string literal in parenthesis.
425 /* Need left parenthesis */
426 if (!ConsumeLParen ()) {
431 if (CurTok.Tok != TOK_SCONST) {
433 /* Print a diagnostic */
434 Error ("String literal expected");
436 /* Try some smart error recovery: Skip tokens until we reach the
437 ** enclosing paren, or a semicolon.
443 /* Parse the ASM statement */
447 /* Closing paren needed */