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 ******************************************************************************/
117 /* Eat the entire line that contains the #line directive */
119 Gbl_LineBufPtr = Gbl_CurrentLineBuffer;
121 while ((c = input()) != '\n' && c != EOF)
128 /* First argument is the actual line number */
130 Token = strtok (Gbl_CurrentLineBuffer, " ");
136 /* First argument is the line number */
138 LineNumber = (UINT32) UtDoConstant (Token);
140 /* Emit the appropriate number of newlines */
142 Gbl_CurrentColumn = 0;
143 if (LineNumber > Gbl_CurrentLineNumber)
145 for (i = 0; i < (LineNumber - Gbl_CurrentLineNumber); i++)
147 FlWriteFile (ASL_FILE_SOURCE_OUTPUT, "\n", 1);
152 FlSetLineNumber (LineNumber);
154 /* Second argument is the optional filename (in double quotes) */
156 Token = strtok (NULL, " \"");
159 Filename = ACPI_ALLOCATE_ZEROED (strlen (Token) + 1);
160 strcpy (Filename, Token);
161 FlSetFilename (Filename);
164 /* Third argument is not supported at this time */
168 /* Reset globals for a new line */
170 Gbl_CurrentLineOffset += Gbl_CurrentColumn;
171 Gbl_CurrentColumn = 0;
172 Gbl_LineBufPtr = Gbl_CurrentLineBuffer;
176 /*******************************************************************************
178 * FUNCTION: AslPopInputFileStack
182 * RETURN: 0 if a node was popped, -1 otherwise
184 * DESCRIPTION: Pop the top of the input file stack and point the parser to
185 * the saved parse buffer contained in the fnode. Also, set the
186 * global line counters to the saved values. This function is
187 * called when an include file reaches EOF.
189 ******************************************************************************/
192 AslPopInputFileStack (
195 ASL_FILE_NODE *Fnode;
198 Fnode = Gbl_IncludeFileStack;
199 DbgPrint (ASL_PARSE_OUTPUT,
200 "\nPop InputFile Stack, Fnode %p\n\n", Fnode);
207 /* Close the current include file */
211 /* Update the top-of-stack */
213 Gbl_IncludeFileStack = Fnode->Next;
215 /* Reset global line counter and filename */
217 Gbl_Files[ASL_FILE_INPUT].Filename = Fnode->Filename;
218 Gbl_CurrentLineNumber = Fnode->CurrentLineNumber;
220 /* Point the parser to the popped file */
222 yy_delete_buffer (YY_CURRENT_BUFFER);
223 yy_switch_to_buffer (Fnode->State);
225 /* All done with this node */
232 /*******************************************************************************
234 * FUNCTION: AslPushInputFileStack
236 * PARAMETERS: InputFile - Open file pointer
237 * Filename - Name of the file
241 * DESCRIPTION: Push the InputFile onto the file stack, and point the parser
242 * to this file. Called when an include file is successfully
245 ******************************************************************************/
248 AslPushInputFileStack (
252 ASL_FILE_NODE *Fnode;
253 YY_BUFFER_STATE State;
256 /* Save the current state in an Fnode */
258 Fnode = UtLocalCalloc (sizeof (ASL_FILE_NODE));
261 Fnode->Next = Gbl_IncludeFileStack;
262 Fnode->State = YY_CURRENT_BUFFER;
263 Fnode->Filename = Gbl_Files[ASL_FILE_INPUT].Filename;
264 Fnode->CurrentLineNumber = Gbl_CurrentLineNumber;
266 /* Push it on the stack */
268 Gbl_IncludeFileStack = Fnode;
270 /* Point the parser to this file */
272 State = yy_create_buffer (InputFile, YY_BUF_SIZE);
273 yy_switch_to_buffer (State);
275 DbgPrint (ASL_PARSE_OUTPUT,
276 "\nPush InputFile Stack, returning %p\n\n", InputFile);
278 /* Reset the global line count and filename */
280 Gbl_Files[ASL_FILE_INPUT].Filename =
281 UtStringCacheCalloc (strlen (Filename) + 1);
283 strcpy (Gbl_Files[ASL_FILE_INPUT].Filename, Filename);
285 Gbl_CurrentLineNumber = 1;
290 /*******************************************************************************
292 * FUNCTION: AslResetCurrentLineBuffer
298 * DESCRIPTION: Reset the Line Buffer to zero, increment global line numbers.
300 ******************************************************************************/
303 AslResetCurrentLineBuffer (
307 if (Gbl_Files[ASL_FILE_SOURCE_OUTPUT].Handle)
309 FlWriteFile (ASL_FILE_SOURCE_OUTPUT, Gbl_CurrentLineBuffer,
310 Gbl_LineBufPtr - Gbl_CurrentLineBuffer);
313 Gbl_CurrentLineOffset += Gbl_CurrentColumn;
314 Gbl_CurrentColumn = 0;
316 Gbl_CurrentLineNumber++;
317 Gbl_LogicalLineNumber++;
318 Gbl_LineBufPtr = Gbl_CurrentLineBuffer;
322 /*******************************************************************************
324 * FUNCTION: AslInsertLineBuffer
326 * PARAMETERS: SourceChar - One char from the input ASL source file
330 * DESCRIPTION: Put one character of the source file into the temp line buffer
332 ******************************************************************************/
335 AslInsertLineBuffer (
342 if (SourceChar == EOF)
347 Gbl_InputByteCount++;
349 /* Handle tabs. Convert to spaces */
351 if (SourceChar == '\t')
354 Count = ASL_SPACES_PER_TAB -
355 (Gbl_CurrentColumn & (ASL_SPACES_PER_TAB-1));
358 for (i = 0; i < Count; i++)
362 /* Insert the character into the line buffer */
364 *Gbl_LineBufPtr = (UINT8) SourceChar;
368 (Gbl_CurrentLineBuffer + (Gbl_LineBufferSize - 1)))
372 * Warning if we have split a long source line.
373 * <Probably overkill>
375 sprintf (MsgBuffer, "Max %u", Gbl_LineBufferSize);
376 AslCommonError (ASL_WARNING, ASL_MSG_LONG_LINE,
377 Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
378 Gbl_CurrentLineOffset, Gbl_CurrentColumn,
379 Gbl_Files[ASL_FILE_INPUT].Filename, MsgBuffer);
382 AslResetCurrentLineBuffer ();
384 else if (SourceChar == '\n')
388 AslResetCurrentLineBuffer ();
394 /*******************************************************************************
398 * PARAMETERS: yytext - Contains the matched keyword.
399 * Type - Keyword/Character type:
400 * 0 = anything except a keyword
401 * 1 = pseudo-keywords
402 * 2 = non-executable ASL keywords
403 * 3 = executable ASL keywords
407 * DESCRIPTION: Count keywords and put them into the line buffer
409 ******************************************************************************/
429 TotalExecutableOpcodes++;
437 for (i = 0; (yytext[i] != 0) && (yytext[i] != EOF); i++)
439 AslInsertLineBuffer (yytext[i]);
445 /*******************************************************************************
447 * FUNCTION: AslDoComment
453 * DESCRIPTION: Process a standard comment.
455 ******************************************************************************/
465 AslInsertLineBuffer ('/');
466 AslInsertLineBuffer ('*');
470 /* Eat chars until end-of-comment */
472 while (((c = input ()) != '*') && (c != EOF))
474 AslInsertLineBuffer (c);
484 * Check for nested comment -- can help catch cases where a previous
485 * comment was accidently left unterminated
487 if ((c1 == '/') && (c == '*'))
489 AslCommonError (ASL_WARNING, ASL_MSG_NESTED_COMMENT,
490 Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
491 Gbl_InputByteCount, Gbl_CurrentColumn,
492 Gbl_Files[ASL_FILE_INPUT].Filename, NULL);
495 /* Comment is closed only if the NEXT character is a slash */
497 AslInsertLineBuffer (c);
499 if (((c1 = input ()) != '/') && (c1 != EOF))
510 AslInsertLineBuffer (c1);
516 * Premature End-Of-File
518 AslCommonError (ASL_ERROR, ASL_MSG_EARLY_EOF,
519 Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
520 Gbl_CurrentLineOffset, Gbl_CurrentColumn,
521 Gbl_Files[ASL_FILE_INPUT].Filename, NULL);
526 /*******************************************************************************
528 * FUNCTION: AslDoCommentType2
534 * DESCRIPTION: Process a new "//" comment.
536 ******************************************************************************/
545 AslInsertLineBuffer ('/');
546 AslInsertLineBuffer ('/');
548 while (((c = input ()) != '\n') && (c != EOF))
550 AslInsertLineBuffer (c);
555 /* End of file is OK, change to newline. Let parser detect EOF later */
560 AslInsertLineBuffer (c);
565 /*******************************************************************************
567 * FUNCTION: AslDoStringLiteral
573 * DESCRIPTION: Process a string literal (surrounded by quotes)
575 ******************************************************************************/
581 char *StringBuffer = MsgBuffer;
582 char *EndBuffer = MsgBuffer + ASL_MSG_BUFFER_SIZE;
585 UINT32 State = ASL_NORMAL_CHAR;
588 char ConvertBuffer[4];
592 * Eat chars until end-of-literal.
593 * NOTE: Put back the original surrounding quotes into the
594 * source line buffer.
596 AslInsertLineBuffer ('\"');
597 while ((StringChar = input()) != EOF)
599 AslInsertLineBuffer (StringChar);
604 case ASL_NORMAL_CHAR:
610 * Special handling for backslash-escape sequence. We will
611 * toss the backslash and translate the escape char(s).
613 State = ASL_ESCAPE_SEQUENCE;
618 /* String terminator */
620 goto CompletedString;
629 case ASL_ESCAPE_SEQUENCE:
631 State = ASL_NORMAL_CHAR;
636 StringChar = 0x07; /* BELL */
641 StringChar = 0x08; /* BACKSPACE */
646 StringChar = 0x0C; /* FORMFEED */
651 StringChar = 0x0A; /* LINEFEED */
656 StringChar = 0x0D; /* CARRIAGE RETURN*/
661 StringChar = 0x09; /* HORIZONTAL TAB */
666 StringChar = 0x0B; /* VERTICAL TAB */
671 State = ASL_HEX_CONSTANT;
675 case '\'': /* Single Quote */
676 case '\"': /* Double Quote */
677 case '\\': /* Backslash */
683 /* Check for an octal digit (0-7) */
685 if (ACPI_IS_OCTAL_DIGIT (StringChar))
687 State = ASL_OCTAL_CONSTANT;
688 ConvertBuffer[0] = StringChar;
693 /* Unknown escape sequence issue warning, but use the character */
695 AslCommonError (ASL_WARNING, ASL_MSG_INVALID_ESCAPE,
696 Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
697 Gbl_CurrentLineOffset, Gbl_CurrentColumn,
698 Gbl_Files[ASL_FILE_INPUT].Filename, NULL);
704 case ASL_OCTAL_CONSTANT:
706 /* Up to three octal digits allowed */
708 if (!ACPI_IS_OCTAL_DIGIT (StringChar) ||
712 * Reached end of the constant. Convert the assembled ASCII
713 * string and resume processing of the next character
715 ConvertBuffer[i] = 0;
716 Digit = (UINT8) ACPI_STRTOUL (ConvertBuffer, NULL, 8);
718 /* Check for NULL or non-ascii character (ignore if so) */
720 if ((Digit == 0) || (Digit > ACPI_ASCII_MAX))
722 AslCommonError (ASL_WARNING, ASL_MSG_INVALID_STRING,
723 Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
724 Gbl_CurrentLineOffset, Gbl_CurrentColumn,
725 Gbl_Files[ASL_FILE_INPUT].Filename, NULL);
729 *StringBuffer = (char) Digit;
731 if (StringBuffer >= EndBuffer)
737 State = ASL_NORMAL_CHAR;
742 /* Append another digit of the constant */
744 ConvertBuffer[i] = StringChar;
748 case ASL_HEX_CONSTANT:
750 /* Up to two hex digits allowed */
752 if (!ACPI_IS_XDIGIT (StringChar) ||
756 * Reached end of the constant. Convert the assembled ASCII
757 * string and resume processing of the next character
759 ConvertBuffer[i] = 0;
760 Digit = (UINT8) ACPI_STRTOUL (ConvertBuffer, NULL, 16);
762 /* Check for NULL or non-ascii character (ignore if so) */
764 if ((Digit == 0) || (Digit > ACPI_ASCII_MAX))
766 AslCommonError (ASL_WARNING, ASL_MSG_INVALID_STRING,
767 Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
768 Gbl_CurrentLineOffset, Gbl_CurrentColumn,
769 Gbl_Files[ASL_FILE_INPUT].Filename, NULL);
773 *StringBuffer = (char) Digit;
775 if (StringBuffer >= EndBuffer)
781 State = ASL_NORMAL_CHAR;
786 /* Append another digit of the constant */
788 ConvertBuffer[i] = StringChar;
797 /* Save the finished character */
799 *StringBuffer = StringChar;
801 if (StringBuffer >= EndBuffer)
808 * Premature End-Of-File
810 AslCommonError (ASL_ERROR, ASL_MSG_EARLY_EOF,
811 Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
812 Gbl_CurrentLineOffset, Gbl_CurrentColumn,
813 Gbl_Files[ASL_FILE_INPUT].Filename, NULL);
819 * Null terminate the input string and copy string to a new buffer
823 CleanString = UtStringCacheCalloc (strlen (MsgBuffer) + 1);
826 AslCommonError (ASL_ERROR, ASL_MSG_MEMORY_ALLOCATION,
827 Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
828 Gbl_CurrentLineOffset, Gbl_CurrentColumn,
829 Gbl_Files[ASL_FILE_INPUT].Filename, NULL);
833 ACPI_STRCPY (CleanString, MsgBuffer);
834 AslCompilerlval.s = CleanString;
840 /* Literal was too long */
842 AslCommonError (ASL_ERROR, ASL_MSG_STRING_LENGTH,
843 Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
844 Gbl_CurrentLineOffset, Gbl_CurrentColumn,
845 Gbl_Files[ASL_FILE_INPUT].Filename, "Max length 4096");