1 /******************************************************************************
3 * Module Name: aslsupport.l - Flex/lex scanner C support routines.
4 * NOTE: Included into aslcompile.l, not compiled by itself.
6 *****************************************************************************/
9 * Copyright (C) 2000 - 2015, Intel Corp.
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions, and the following disclaimer,
17 * without modification.
18 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19 * substantially similar to the "NO WARRANTY" disclaimer below
20 * ("Disclaimer") and any redistribution must be conditioned upon
21 * including a substantially similar Disclaimer requirement for further
22 * binary redistribution.
23 * 3. Neither the names of the above-listed copyright holders nor the names
24 * of any contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * Alternatively, this software may be distributed under the terms of the
28 * GNU General Public License ("GPL") version 2 as published by the Free
29 * Software Foundation.
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 * POSSIBILITY OF SUCH DAMAGES.
47 #define ASL_SPACES_PER_TAB 4
49 #define ASL_NORMAL_CHAR 0
50 #define ASL_ESCAPE_SEQUENCE 1
51 #define ASL_OCTAL_CONSTANT 2
52 #define ASL_HEX_CONSTANT 3
55 /* File node - used for "Include" operator file stack */
57 typedef struct asl_file_node
60 UINT32 CurrentLineNumber;
61 YY_BUFFER_STATE State;
63 struct asl_file_node *Next;
67 /* File stack for the "Include" operator (NOT #include operator) */
69 ASL_FILE_NODE *Gbl_IncludeFileStack = NULL;
72 /*******************************************************************************
74 * FUNCTION: AslParserCleanup
76 * Used to delete the current buffer
78 ******************************************************************************/
85 yy_delete_buffer (YY_CURRENT_BUFFER);
89 /*******************************************************************************
91 * FUNCTION: AslDoLineDirective
93 * PARAMETERS: None. Uses input() to access current source code line
95 * RETURN: Updates global line number and filename
97 * DESCRIPTION: Handle #line directives emitted by the preprocessor.
99 * The #line directive is emitted by the preprocesser, and is used to
100 * pass through line numbers from the original source code file to the
101 * preprocessor output file (.i). This allows any compiler-generated
102 * error messages to be displayed with the correct line number.
104 ******************************************************************************/
116 Gbl_HasIncludeFiles = TRUE;
118 /* Eat the entire line that contains the #line directive */
120 Gbl_LineBufPtr = Gbl_CurrentLineBuffer;
122 while ((c = input()) != '\n' && c != EOF)
129 /* First argument is the actual line number */
131 Token = strtok (Gbl_CurrentLineBuffer, " ");
137 /* First argument is the line number */
139 LineNumber = (UINT32) UtDoConstant (Token);
141 /* Emit the appropriate number of newlines */
143 Gbl_CurrentColumn = 0;
144 if (LineNumber > Gbl_CurrentLineNumber)
146 for (i = 0; i < (LineNumber - Gbl_CurrentLineNumber); i++)
148 FlWriteFile (ASL_FILE_SOURCE_OUTPUT, "\n", 1);
153 FlSetLineNumber (LineNumber);
155 /* Second argument is the optional filename (in double quotes) */
157 Token = strtok (NULL, " \"");
160 Filename = ACPI_ALLOCATE_ZEROED (strlen (Token) + 1);
161 strcpy (Filename, Token);
162 FlSetFilename (Filename);
165 /* Third argument is not supported at this time */
169 /* Reset globals for a new line */
171 Gbl_CurrentLineOffset += Gbl_CurrentColumn;
172 Gbl_CurrentColumn = 0;
173 Gbl_LineBufPtr = Gbl_CurrentLineBuffer;
177 /*******************************************************************************
179 * FUNCTION: AslPopInputFileStack
183 * RETURN: 0 if a node was popped, -1 otherwise
185 * DESCRIPTION: Pop the top of the input file stack and point the parser to
186 * the saved parse buffer contained in the fnode. Also, set the
187 * global line counters to the saved values. This function is
188 * called when an include file reaches EOF.
190 ******************************************************************************/
193 AslPopInputFileStack (
196 ASL_FILE_NODE *Fnode;
199 Fnode = Gbl_IncludeFileStack;
200 DbgPrint (ASL_PARSE_OUTPUT,
201 "\nPop InputFile Stack, Fnode %p\n\n", Fnode);
208 /* Close the current include file */
212 /* Update the top-of-stack */
214 Gbl_IncludeFileStack = Fnode->Next;
216 /* Reset global line counter and filename */
218 Gbl_Files[ASL_FILE_INPUT].Filename = Fnode->Filename;
219 Gbl_CurrentLineNumber = Fnode->CurrentLineNumber;
221 /* Point the parser to the popped file */
223 yy_delete_buffer (YY_CURRENT_BUFFER);
224 yy_switch_to_buffer (Fnode->State);
226 /* All done with this node */
233 /*******************************************************************************
235 * FUNCTION: AslPushInputFileStack
237 * PARAMETERS: InputFile - Open file pointer
238 * Filename - Name of the file
242 * DESCRIPTION: Push the InputFile onto the file stack, and point the parser
243 * to this file. Called when an include file is successfully
246 ******************************************************************************/
249 AslPushInputFileStack (
253 ASL_FILE_NODE *Fnode;
254 YY_BUFFER_STATE State;
257 /* Save the current state in an Fnode */
259 Fnode = UtLocalCalloc (sizeof (ASL_FILE_NODE));
262 Fnode->Next = Gbl_IncludeFileStack;
263 Fnode->State = YY_CURRENT_BUFFER;
264 Fnode->Filename = Gbl_Files[ASL_FILE_INPUT].Filename;
265 Fnode->CurrentLineNumber = Gbl_CurrentLineNumber;
267 /* Push it on the stack */
269 Gbl_IncludeFileStack = Fnode;
271 /* Point the parser to this file */
273 State = yy_create_buffer (InputFile, YY_BUF_SIZE);
274 yy_switch_to_buffer (State);
276 DbgPrint (ASL_PARSE_OUTPUT,
277 "\nPush InputFile Stack, returning %p\n\n", InputFile);
279 /* Reset the global line count and filename */
281 Gbl_Files[ASL_FILE_INPUT].Filename =
282 UtStringCacheCalloc (strlen (Filename) + 1);
284 strcpy (Gbl_Files[ASL_FILE_INPUT].Filename, Filename);
286 Gbl_CurrentLineNumber = 1;
291 /*******************************************************************************
293 * FUNCTION: AslResetCurrentLineBuffer
299 * DESCRIPTION: Reset the Line Buffer to zero, increment global line numbers.
301 ******************************************************************************/
304 AslResetCurrentLineBuffer (
308 if (Gbl_Files[ASL_FILE_SOURCE_OUTPUT].Handle)
310 FlWriteFile (ASL_FILE_SOURCE_OUTPUT, Gbl_CurrentLineBuffer,
311 Gbl_LineBufPtr - Gbl_CurrentLineBuffer);
314 Gbl_CurrentLineOffset += Gbl_CurrentColumn;
315 Gbl_CurrentColumn = 0;
317 Gbl_CurrentLineNumber++;
318 Gbl_LogicalLineNumber++;
319 Gbl_LineBufPtr = Gbl_CurrentLineBuffer;
323 /*******************************************************************************
325 * FUNCTION: AslInsertLineBuffer
327 * PARAMETERS: SourceChar - One char from the input ASL source file
331 * DESCRIPTION: Put one character of the source file into the temp line buffer
333 ******************************************************************************/
336 AslInsertLineBuffer (
343 if (SourceChar == EOF)
348 Gbl_InputByteCount++;
350 /* Handle tabs. Convert to spaces */
352 if (SourceChar == '\t')
355 Count = ASL_SPACES_PER_TAB -
356 (Gbl_CurrentColumn & (ASL_SPACES_PER_TAB-1));
359 for (i = 0; i < Count; i++)
363 /* Insert the character into the line buffer */
365 *Gbl_LineBufPtr = (UINT8) SourceChar;
369 (Gbl_CurrentLineBuffer + (Gbl_LineBufferSize - 1)))
373 * Warning if we have split a long source line.
374 * <Probably overkill>
376 sprintf (MsgBuffer, "Max %u", Gbl_LineBufferSize);
377 AslCommonError (ASL_WARNING, ASL_MSG_LONG_LINE,
378 Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
379 Gbl_CurrentLineOffset, Gbl_CurrentColumn,
380 Gbl_Files[ASL_FILE_INPUT].Filename, MsgBuffer);
383 AslResetCurrentLineBuffer ();
385 else if (SourceChar == '\n')
389 AslResetCurrentLineBuffer ();
395 /*******************************************************************************
399 * PARAMETERS: yytext - Contains the matched keyword.
400 * Type - Keyword/Character type:
401 * 0 = anything except a keyword
402 * 1 = pseudo-keywords
403 * 2 = non-executable ASL keywords
404 * 3 = executable ASL keywords
408 * DESCRIPTION: Count keywords and put them into the line buffer
410 ******************************************************************************/
430 TotalExecutableOpcodes++;
438 for (i = 0; (yytext[i] != 0) && (yytext[i] != EOF); i++)
440 AslInsertLineBuffer (yytext[i]);
446 /*******************************************************************************
448 * FUNCTION: AslDoComment
454 * DESCRIPTION: Process a standard comment.
456 ******************************************************************************/
466 AslInsertLineBuffer ('/');
467 AslInsertLineBuffer ('*');
471 /* Eat chars until end-of-comment */
473 while (((c = input ()) != '*') && (c != EOF))
475 AslInsertLineBuffer (c);
485 * Check for nested comment -- can help catch cases where a previous
486 * comment was accidently left unterminated
488 if ((c1 == '/') && (c == '*'))
490 AslCommonError (ASL_WARNING, ASL_MSG_NESTED_COMMENT,
491 Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
492 Gbl_InputByteCount, Gbl_CurrentColumn,
493 Gbl_Files[ASL_FILE_INPUT].Filename, NULL);
496 /* Comment is closed only if the NEXT character is a slash */
498 AslInsertLineBuffer (c);
500 if (((c1 = input ()) != '/') && (c1 != EOF))
511 AslInsertLineBuffer (c1);
517 * Premature End-Of-File
519 AslCommonError (ASL_ERROR, ASL_MSG_EARLY_EOF,
520 Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
521 Gbl_CurrentLineOffset, Gbl_CurrentColumn,
522 Gbl_Files[ASL_FILE_INPUT].Filename, NULL);
527 /*******************************************************************************
529 * FUNCTION: AslDoCommentType2
535 * DESCRIPTION: Process a new "//" comment.
537 ******************************************************************************/
546 AslInsertLineBuffer ('/');
547 AslInsertLineBuffer ('/');
549 while (((c = input ()) != '\n') && (c != EOF))
551 AslInsertLineBuffer (c);
556 /* End of file is OK, change to newline. Let parser detect EOF later */
561 AslInsertLineBuffer (c);
566 /*******************************************************************************
568 * FUNCTION: AslDoStringLiteral
574 * DESCRIPTION: Process a string literal (surrounded by quotes)
576 ******************************************************************************/
582 char *StringBuffer = MsgBuffer;
583 char *EndBuffer = MsgBuffer + ASL_MSG_BUFFER_SIZE;
586 UINT32 State = ASL_NORMAL_CHAR;
589 char ConvertBuffer[4];
593 * Eat chars until end-of-literal.
594 * NOTE: Put back the original surrounding quotes into the
595 * source line buffer.
597 AslInsertLineBuffer ('\"');
598 while ((StringChar = input()) != EOF)
600 AslInsertLineBuffer (StringChar);
605 case ASL_NORMAL_CHAR:
611 * Special handling for backslash-escape sequence. We will
612 * toss the backslash and translate the escape char(s).
614 State = ASL_ESCAPE_SEQUENCE;
619 /* String terminator */
621 goto CompletedString;
630 case ASL_ESCAPE_SEQUENCE:
632 State = ASL_NORMAL_CHAR;
637 StringChar = 0x07; /* BELL */
642 StringChar = 0x08; /* BACKSPACE */
647 StringChar = 0x0C; /* FORMFEED */
652 StringChar = 0x0A; /* LINEFEED */
657 StringChar = 0x0D; /* CARRIAGE RETURN*/
662 StringChar = 0x09; /* HORIZONTAL TAB */
667 StringChar = 0x0B; /* VERTICAL TAB */
672 State = ASL_HEX_CONSTANT;
676 case '\'': /* Single Quote */
677 case '\"': /* Double Quote */
678 case '\\': /* Backslash */
684 /* Check for an octal digit (0-7) */
686 if (ACPI_IS_OCTAL_DIGIT (StringChar))
688 State = ASL_OCTAL_CONSTANT;
689 ConvertBuffer[0] = StringChar;
694 /* Unknown escape sequence issue warning, but use the character */
696 AslCommonError (ASL_WARNING, ASL_MSG_INVALID_ESCAPE,
697 Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
698 Gbl_CurrentLineOffset, Gbl_CurrentColumn,
699 Gbl_Files[ASL_FILE_INPUT].Filename, NULL);
705 case ASL_OCTAL_CONSTANT:
707 /* Up to three octal digits allowed */
709 if (!ACPI_IS_OCTAL_DIGIT (StringChar) ||
713 * Reached end of the constant. Convert the assembled ASCII
714 * string and resume processing of the next character
716 ConvertBuffer[i] = 0;
717 Digit = (UINT8) strtoul (ConvertBuffer, NULL, 8);
719 /* Check for NULL or non-ascii character (ignore if so) */
721 if ((Digit == 0) || (Digit > ACPI_ASCII_MAX))
723 AslCommonError (ASL_WARNING, ASL_MSG_INVALID_STRING,
724 Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
725 Gbl_CurrentLineOffset, Gbl_CurrentColumn,
726 Gbl_Files[ASL_FILE_INPUT].Filename, NULL);
730 *StringBuffer = (char) Digit;
732 if (StringBuffer >= EndBuffer)
738 State = ASL_NORMAL_CHAR;
743 /* Append another digit of the constant */
745 ConvertBuffer[i] = StringChar;
749 case ASL_HEX_CONSTANT:
751 /* Up to two hex digits allowed */
753 if (!isxdigit (StringChar) ||
757 * Reached end of the constant. Convert the assembled ASCII
758 * string and resume processing of the next character
760 ConvertBuffer[i] = 0;
761 Digit = (UINT8) strtoul (ConvertBuffer, NULL, 16);
763 /* Check for NULL or non-ascii character (ignore if so) */
765 if ((Digit == 0) || (Digit > ACPI_ASCII_MAX))
767 AslCommonError (ASL_WARNING, ASL_MSG_INVALID_STRING,
768 Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
769 Gbl_CurrentLineOffset, Gbl_CurrentColumn,
770 Gbl_Files[ASL_FILE_INPUT].Filename, NULL);
774 *StringBuffer = (char) Digit;
776 if (StringBuffer >= EndBuffer)
782 State = ASL_NORMAL_CHAR;
787 /* Append another digit of the constant */
789 ConvertBuffer[i] = StringChar;
798 /* Save the finished character */
800 *StringBuffer = StringChar;
802 if (StringBuffer >= EndBuffer)
809 * Premature End-Of-File
811 AslCommonError (ASL_ERROR, ASL_MSG_EARLY_EOF,
812 Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
813 Gbl_CurrentLineOffset, Gbl_CurrentColumn,
814 Gbl_Files[ASL_FILE_INPUT].Filename, NULL);
820 * Null terminate the input string and copy string to a new buffer
824 CleanString = UtStringCacheCalloc (strlen (MsgBuffer) + 1);
827 AslCommonError (ASL_ERROR, ASL_MSG_MEMORY_ALLOCATION,
828 Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
829 Gbl_CurrentLineOffset, Gbl_CurrentColumn,
830 Gbl_Files[ASL_FILE_INPUT].Filename, NULL);
834 strcpy (CleanString, MsgBuffer);
835 AslCompilerlval.s = CleanString;
841 /* Literal was too long */
843 AslCommonError (ASL_ERROR, ASL_MSG_STRING_LENGTH,
844 Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
845 Gbl_CurrentLineOffset, Gbl_CurrentColumn,
846 Gbl_Files[ASL_FILE_INPUT].Filename, "Max length 4096");