other SUBMIT fix (forgot to commit sigh)

This commit is contained in:
Allen Webster 2016-03-17 17:04:46 -04:00
parent 0f160be414
commit 24a5dd57b6
2 changed files with 298 additions and 250 deletions

View File

@ -406,12 +406,9 @@ enum Lex_PP_State{
LSPP_count LSPP_count
}; };
struct Lex_FSM{ enum Pos_Update_Rule{
char state; PUR_none,
char int_state; PUR_unget_whitespace
char emit_token;
char multi_line;
char completed;
}; };
lexer_link Lex_PP_State lexer_link Lex_PP_State
@ -496,30 +493,36 @@ cpp_push_token_nonalloc(Cpp_Token *out_tokens, int *token_i, Cpp_Token token){
} }
} }
struct Lex_FSM{
unsigned char state;
unsigned char int_state;
unsigned char emit_token;
unsigned char multi_line;
unsigned char completed;
};
struct Lex_Data{ struct Lex_Data{
Lex_FSM fsm; Lex_FSM fsm;
char pp_state; unsigned char pp_state;
char completed; unsigned char completed;
int token_start; int token_start;
}; };
lexer_link Lex_Data lexer_link Lex_Data
cpp_lex_nonalloc(Lex_Data lex_data, char *chunk, int file_absolute_pos, int size, int last_chunk, Cpp_Token_Stack *token_stack_out){ cpp_lex_nonalloc(Lex_Data S, char *chunk, int file_absolute_pos, int size, Cpp_Token_Stack *token_stack_out){
Cpp_Token *out_tokens = token_stack_out->tokens; Cpp_Token *out_tokens = token_stack_out->tokens;
int token_i = token_stack_out->count; int token_i = token_stack_out->count;
int max_token_i = token_stack_out->max_count; int max_token_i = token_stack_out->max_count;
Cpp_Token token = {}; Cpp_Token token = {(Cpp_Token_Type)0};
Lex_FSM fsm = {0};
int pos = file_absolute_pos; int pos = file_absolute_pos;
int end_pos = size + file_absolute_pos; int end_pos = size + file_absolute_pos;
int stream_end_pos = 0x7FFFFFFF; int restore_point = 0;
char c = 0; char c = 0;
if (last_chunk){ Pos_Update_Rule pos_update_rule;
stream_end_pos = end_pos;
++end_pos;
}
chunk -= file_absolute_pos; chunk -= file_absolute_pos;
@ -530,106 +533,96 @@ cpp_lex_nonalloc(Lex_Data lex_data, char *chunk, int file_absolute_pos, int size
if (c == ' ' || c == '\n' || c == '\t' || c == '\r' || c == '\f' || c == '\v'){ if (c == ' ' || c == '\n' || c == '\t' || c == '\r' || c == '\f' || c == '\v'){
for (; pos < end_pos;){ for (; pos < end_pos;){
c = chunk[pos++]; c = chunk[pos++];
if (lex_data.pp_state != LSPP_default){ if (S.pp_state != LSPP_default){
if (c == '\n') lex_data.pp_state = LSPP_default; if (c == '\n') S.pp_state = LSPP_default;
} }
if (!(c == ' ' || c == '\n' || c == '\t' || c == '\r' || c == '\f' || c == '\v')) break; if (!(c == ' ' || c == '\n' || c == '\t' || c == '\r' || c == '\f' || c == '\v')) break;
} }
--pos; --pos;
} }
lex_data.token_start = pos; S.token_start = pos;
lex_data.fsm = {0}; S.fsm = {0};
for (; lex_data.fsm.emit_token == 0 && pos < end_pos;){ for (; S.fsm.emit_token == 0 && pos < end_pos;){
if (pos < stream_end_pos){ c = chunk[pos++];
c = chunk[pos++];
}
else{
c = 0;
++pos;
}
{ {
char pp_state = lex_data.pp_state; fsm = S.fsm;
char state = lex_data.fsm.state;
char emit_token = lex_data.fsm.emit_token;
char multi_line = lex_data.fsm.multi_line;
switch (pp_state){ switch (S.pp_state){
case LSPP_error: case LSPP_error:
state = LS_error_message; fsm.state = LS_error_message;
if (c == '\n') emit_token = 1; if (c == '\n') fsm.emit_token = 1;
break; break;
case LSPP_include: case LSPP_include:
switch (state){ switch (fsm.state){
case LSINC_default: case LSINC_default:
switch (c){ switch (c){
case '"': state = LSINC_quotes; break; case '"': fsm.state = LSINC_quotes; break;
case '<': state = LSINC_pointy; break; case '<': fsm.state = LSINC_pointy; break;
default: state = LSINC_junk; break; default: fsm.state = LSINC_junk; break;
} }
break; break;
case LSINC_quotes: case LSINC_quotes:
if (c == '"') emit_token = 1; if (c == '"') fsm.emit_token = 1;
break; break;
case LSINC_pointy: case LSINC_pointy:
if (c == '>') emit_token = 1; if (c == '>') fsm.emit_token = 1;
break; break;
case LSINC_junk: case LSINC_junk:
if (c == '\n') emit_token = 1; if (c == '\n') fsm.emit_token = 1;
break; break;
} }
break; break;
default: default:
switch (state){ switch (fsm.state){
case LS_default: case LS_default:
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_'){ if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_'){
state = LS_identifier; fsm.state = LS_identifier;
} }
else if (c >= '1' && c <= '9'){ else if (c >= '1' && c <= '9'){
state = LS_number; fsm.state = LS_number;
} }
else if (c == '0'){ else if (c == '0'){
state = LS_number0; fsm.state = LS_number0;
} }
else switch (c){ else switch (c){
case '\'': state = LS_char; break; case '\'': fsm.state = LS_char; break;
case '"': state = LS_string; break; case '"': fsm.state = LS_string; break;
case '/': state = LS_comment_pre; break; case '/': fsm.state = LS_comment_pre; break;
case '.': state = LS_dot; break; case '.': fsm.state = LS_dot; break;
case '<': state = LS_less; break; case '<': fsm.state = LS_less; break;
case '>': state = LS_more; break; case '>': fsm.state = LS_more; break;
case '-': state = LS_minus; break; case '-': fsm.state = LS_minus; break;
case '&': state = LS_and; break; case '&': fsm.state = LS_and; break;
case '|': state = LS_or; break; case '|': fsm.state = LS_or; break;
case '+': state = LS_plus; break; case '+': fsm.state = LS_plus; break;
case ':': state = LS_colon; break; case ':': fsm.state = LS_colon; break;
case '*': state = LS_star; break; case '*': fsm.state = LS_star; break;
case '%': state = LS_modulo; break; case '%': fsm.state = LS_modulo; break;
case '^': state = LS_caret; break; case '^': fsm.state = LS_caret; break;
case '=': state = LS_eq; break; case '=': fsm.state = LS_eq; break;
case '!': state = LS_bang; break; case '!': fsm.state = LS_bang; break;
case '#': state = LS_pound; break; case '#': fsm.state = LS_pound; break;
#define OperCase(op,type) case op: emit_token = 1; break; #define OperCase(op,type) case op: fsm.emit_token = 1; break;
OperCase('{', CPP_TOKEN_BRACE_OPEN); OperCase('{', CPP_TOKEN_BRACE_OPEN);
OperCase('}', CPP_TOKEN_BRACE_CLOSE); OperCase('}', CPP_TOKEN_BRACE_CLOSE);
@ -653,98 +646,98 @@ cpp_lex_nonalloc(Lex_Data lex_data, char *chunk, int file_absolute_pos, int size
case LS_identifier: case LS_identifier:
if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_')){ if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_')){
emit_token = 1; fsm.emit_token = 1;
} }
break; break;
case LS_pound: case LS_pound:
if (pp_state == LSPP_default){ if (S.pp_state == LSPP_default){
if (c == ' ' || c == '\t' || c == '\r' || c == '\f' || c == '\v'){ if (c == ' ' || c == '\t' || c == '\r' || c == '\f' || c == '\v'){
state = LS_pound; fsm.state = LS_pound;
} }
else if (c == '\n'){ else if (c == '\n'){
emit_token = 1; fsm.emit_token = 1;
} }
else{ else{
state = LS_pp; fsm.state = LS_pp;
} }
} }
else{ else{
switch (c){ switch (c){
case '#': emit_token = 1; break; case '#': fsm.emit_token = 1; break;
default: emit_token = 1; break; default: fsm.emit_token = 1; break;
} }
} }
break; break;
case LS_pp: case LS_pp:
if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_')){ if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_')){
emit_token = 1; fsm.emit_token = 1;
} }
break; break;
case LS_char: case LS_char:
switch(c){ switch(c){
case '\'': emit_token = 1; break; case '\'': fsm.emit_token = 1; break;
case '\\': state = LS_char_slashed; break; case '\\': fsm.state = LS_char_slashed; break;
} }
break; break;
case LS_char_slashed: case LS_char_slashed:
switch (c){ switch (c){
case '\r': case '\f': case '\v': break; case '\r': case '\f': case '\v': break;
case '\n': state = LS_string; multi_line |= 1; break; case '\n': fsm.state = LS_string; fsm.multi_line |= 1; break;
default: state = LS_char; break; default: fsm.state = LS_char; break;
} }
break; break;
case LS_string: case LS_string:
switch(c){ switch(c){
case '\"': emit_token = 1; break; case '\"': fsm.emit_token = 1; break;
case '\\': state = LS_string_slashed; break; case '\\': fsm.state = LS_string_slashed; break;
} }
break; break;
case LS_string_slashed: case LS_string_slashed:
switch (c){ switch (c){
case '\r': case '\f': case '\v': break; case '\r': case '\f': case '\v': break;
case '\n': state = LS_string; multi_line |= 1; break; case '\n': fsm.state = LS_string; fsm.multi_line |= 1; break;
default: state = LS_string; break; default: fsm.state = LS_string; break;
} }
break; break;
case LS_number: case LS_number:
if (c >= '0' && c <= '9'){ if (c >= '0' && c <= '9'){
state = LS_number; fsm.state = LS_number;
} }
else{ else{
switch (c){ switch (c){
case '.': state = LS_float; break; case '.': fsm.state = LS_float; break;
default: emit_token = 1; break; default: fsm.emit_token = 1; break;
} }
} }
break; break;
case LS_number0: case LS_number0:
if (c >= '0' && c <= '9'){ if (c >= '0' && c <= '9'){
state = LS_number; fsm.state = LS_number;
} }
else if (c == 'x'){ else if (c == 'x'){
state = LS_hex; fsm.state = LS_hex;
} }
else if (c == '.'){ else if (c == '.'){
state = LS_float; fsm.state = LS_float;
} }
else{ else{
emit_token = 1; fsm.emit_token = 1;
} }
break; break;
case LS_float: case LS_float:
if (!(c >= '0' && c <= '9')){ if (!(c >= '0' && c <= '9')){
switch (c){ switch (c){
case 'e': state = LS_crazy_float0; break; case 'e': fsm.state = LS_crazy_float0; break;
default: emit_token = 1; break; default: fsm.emit_token = 1; break;
} }
} }
break; break;
@ -752,10 +745,10 @@ cpp_lex_nonalloc(Lex_Data lex_data, char *chunk, int file_absolute_pos, int size
case LS_crazy_float0: case LS_crazy_float0:
{ {
if ((c >= '0' && c <= '9') || c == '-'){ if ((c >= '0' && c <= '9') || c == '-'){
state = LS_crazy_float1; fsm.state = LS_crazy_float1;
} }
else{ else{
emit_token = 1; fsm.emit_token = 1;
} }
} }
break; break;
@ -763,194 +756,191 @@ cpp_lex_nonalloc(Lex_Data lex_data, char *chunk, int file_absolute_pos, int size
case LS_crazy_float1: case LS_crazy_float1:
{ {
if (!(c >= '0' && c <= '9')){ if (!(c >= '0' && c <= '9')){
emit_token = 1; fsm.emit_token = 1;
} }
} }
break; break;
case LS_hex: case LS_hex:
if (!(c >= '0' && c <= '9' || c >= 'a' && c <= 'f' || c >= 'A' && c <= 'F')){ if (!(c >= '0' && c <= '9' || c >= 'a' && c <= 'f' || c >= 'A' && c <= 'F')){
emit_token = 1; fsm.emit_token = 1;
} }
break; break;
case LS_dot: case LS_dot:
if (c >= '0' && c <= '9'){ if (c >= '0' && c <= '9'){
state = LS_float; fsm.state = LS_float;
} }
else else
switch (c){ switch (c){
case '.': state = LS_ellipsis; break; case '.': fsm.state = LS_ellipsis; break;
case '*': emit_token = 1; break; case '*': fsm.emit_token = 1; break;
default: emit_token = 1; break; default: fsm.emit_token = 1; break;
} }
break; break;
case LS_ellipsis: emit_token = 1; break; case LS_ellipsis: fsm.emit_token = 1; break;
case LS_less: case LS_less:
switch (c){ switch (c){
case '<': state = LS_less_less; break; case '<': fsm.state = LS_less_less; break;
case '=': emit_token = 1; break; case '=': fsm.emit_token = 1; break;
default: emit_token = 1; break; default: fsm.emit_token = 1; break;
} }
break; break;
case LS_less_less: case LS_less_less:
switch (c){ switch (c){
case '=': emit_token = 1; break; case '=': fsm.emit_token = 1; break;
default: emit_token = 1; break; default: fsm.emit_token = 1; break;
} }
break; break;
case LS_more: case LS_more:
switch (c){ switch (c){
case '>': state = LS_more_more; break; case '>': fsm.state = LS_more_more; break;
case '=': emit_token = 1; break; case '=': fsm.emit_token = 1; break;
default: emit_token = 1; break; default: fsm.emit_token = 1; break;
} }
break; break;
case LS_more_more: case LS_more_more:
switch (c){ switch (c){
case '=': emit_token = 1; break; case '=': fsm.emit_token = 1; break;
default: emit_token = 1; break; default: fsm.emit_token = 1; break;
} }
break; break;
case LS_comment_pre: case LS_comment_pre:
switch (c){ switch (c){
case '/': state = LS_comment; break; case '/': fsm.state = LS_comment; break;
case '*': state = LS_comment_block; break; case '*': fsm.state = LS_comment_block; break;
case '=': emit_token = 1; break; case '=': fsm.emit_token = 1; break;
default: emit_token = 1; break; default: fsm.emit_token = 1; break;
} }
break; break;
case LS_comment: case LS_comment:
switch (c){ switch (c){
case '\\': state = LS_comment_slashed; break; case '\\': fsm.state = LS_comment_slashed; break;
case '\n': emit_token = 1; break; case '\n': fsm.emit_token = 1; break;
} }
break; break;
case LS_comment_slashed: case LS_comment_slashed:
switch (c){ switch (c){
case '\r': case '\f': case '\v': break; case '\r': case '\f': case '\v': break;
default: state = LS_comment; break; default: fsm.state = LS_comment; break;
} }
break; break;
case LS_comment_block: case LS_comment_block:
switch (c){ switch (c){
case '*': state = LS_comment_block_ending; break; case '*': fsm.state = LS_comment_block_ending; break;
} }
break; break;
case LS_comment_block_ending: case LS_comment_block_ending:
switch (c){ switch (c){
case '*': state = LS_comment_block_ending; break; case '*': fsm.state = LS_comment_block_ending; break;
case '/': emit_token = 1; break; case '/': fsm.emit_token = 1; break;
default: state = LS_comment_block; break; default: fsm.state = LS_comment_block; break;
} }
break; break;
case LS_minus: case LS_minus:
switch (c){ switch (c){
case '>': state = LS_arrow; break; case '>': fsm.state = LS_arrow; break;
case '-': emit_token = 1; break; case '-': fsm.emit_token = 1; break;
case '=': emit_token = 1; break; case '=': fsm.emit_token = 1; break;
default: emit_token = 1; break; default: fsm.emit_token = 1; break;
} }
break; break;
case LS_arrow: case LS_arrow:
switch (c){ switch (c){
case '*': emit_token = 1; break; case '*': fsm.emit_token = 1; break;
default: emit_token = 1; break; default: fsm.emit_token = 1; break;
} }
break; break;
case LS_and: case LS_and:
switch (c){ switch (c){
case '&': emit_token = 1; break; case '&': fsm.emit_token = 1; break;
case '=': emit_token = 1; break; case '=': fsm.emit_token = 1; break;
default: emit_token = 1; break; default: fsm.emit_token = 1; break;
} }
break; break;
case LS_or: case LS_or:
switch (c){ switch (c){
case '|': emit_token = 1; break; case '|': fsm.emit_token = 1; break;
case '=': emit_token = 1; break; case '=': fsm.emit_token = 1; break;
default: emit_token = 1; break; default: fsm.emit_token = 1; break;
} }
break; break;
case LS_plus: case LS_plus:
switch (c){ switch (c){
case '+': emit_token = 1; break; case '+': fsm.emit_token = 1; break;
case '=': emit_token = 1; break; case '=': fsm.emit_token = 1; break;
default: emit_token = 1; break; default: fsm.emit_token = 1; break;
} }
break; break;
case LS_colon: case LS_colon:
switch (c){ switch (c){
case ':': emit_token = 1; break; case ':': fsm.emit_token = 1; break;
default: emit_token = 1; break; default: fsm.emit_token = 1; break;
} }
break; break;
case LS_star: case LS_star:
switch (c){ switch (c){
case '=': emit_token = 1; break; case '=': fsm.emit_token = 1; break;
default: emit_token = 1; break; default: fsm.emit_token = 1; break;
} }
break; break;
case LS_modulo: case LS_modulo:
switch (c){ switch (c){
case '=': emit_token = 1; break; case '=': fsm.emit_token = 1; break;
default: emit_token = 1; break; default: fsm.emit_token = 1; break;
} }
break; break;
case LS_caret: case LS_caret:
switch (c){ switch (c){
case '=': emit_token = 1; break; case '=': fsm.emit_token = 1; break;
default: emit_token = 1; break; default: fsm.emit_token = 1; break;
} }
break; break;
case LS_eq: case LS_eq:
switch (c){ switch (c){
case '=': emit_token = 1; break; case '=': fsm.emit_token = 1; break;
default: emit_token = 1; break; default: fsm.emit_token = 1; break;
} }
break; break;
case LS_bang: case LS_bang:
switch (c){ switch (c){
case '=': emit_token = 1; break; case '=': fsm.emit_token = 1; break;
default: emit_token = 1; break; default: fsm.emit_token = 1; break;
} }
break; break;
} }
break; break;
} }
lex_data.pp_state = pp_state; S.fsm = fsm;
lex_data.fsm.state = state;
lex_data.fsm.emit_token = emit_token;
lex_data.fsm.multi_line = multi_line;
} }
} }
if (lex_data.fsm.emit_token){ pos_update_rule = PUR_none;
if (lex_data.pp_state == LSPP_include){ if (S.fsm.emit_token){
switch (lex_data.fsm.state){ if (S.pp_state == LSPP_include){
switch (S.fsm.state){
case LSINC_default:break; case LSINC_default:break;
case LSINC_quotes: case LSINC_quotes:
@ -965,7 +955,7 @@ cpp_lex_nonalloc(Lex_Data lex_data, char *chunk, int file_absolute_pos, int size
break; break;
} }
} }
else switch (lex_data.fsm.state){ else switch (S.fsm.state){
case LS_default: case LS_default:
switch (c){ switch (c){
#define OperCase(op,t) case op: token.type = t; break; #define OperCase(op,t) case op: token.type = t; break;
@ -986,19 +976,19 @@ cpp_lex_nonalloc(Lex_Data lex_data, char *chunk, int file_absolute_pos, int size
OperCase('@', CPP_TOKEN_JUNK); OperCase('@', CPP_TOKEN_JUNK);
OperCase('$', CPP_TOKEN_JUNK); OperCase('$', CPP_TOKEN_JUNK);
#undef OperCase #undef OperCase
case '\\': case '\\':
if (lex_data.pp_state == LSPP_default){ if (S.pp_state == LSPP_default){
token.type = CPP_TOKEN_JUNK; token.type = CPP_TOKEN_JUNK;
} }
else{ else{
int restore_point = pos; restore_point = pos;
c = chunk[pos]; c = chunk[pos];
while (c == ' ' || c == '\t' || c == '\r' || c == '\v' || c == '\f'){ while (c == ' ' || c == '\t' || c == '\r' || c == '\v' || c == '\f'){
c = chunk[pos++]; c = chunk[pos++];
} }
if (c == '\n'){ if (c == '\n'){
lex_data.fsm.emit_token = 0; S.fsm.emit_token = 0;
} }
else{ else{
pos = restore_point; pos = restore_point;
@ -1016,11 +1006,11 @@ cpp_lex_nonalloc(Lex_Data lex_data, char *chunk, int file_absolute_pos, int size
{ {
--pos; --pos;
int start = lex_data.token_start; int start = S.token_start;
int word_size = pos - lex_data.token_start; int word_size = pos - S.token_start;
if (lex_data.pp_state == LSPP_body_if){ if (S.pp_state == LSPP_body_if){
if (match(make_string(chunk + start, word_size), make_lit_string("defined"))){ if (match(make_string(chunk + start, word_size), make_lit_string("defined"))){
token.type = CPP_TOKEN_DEFINED; token.type = CPP_TOKEN_DEFINED;
token.flags = CPP_TFLAG_IS_OPERATOR | CPP_TFLAG_IS_KEYWORD; token.flags = CPP_TFLAG_IS_OPERATOR | CPP_TFLAG_IS_KEYWORD;
@ -1064,7 +1054,7 @@ cpp_lex_nonalloc(Lex_Data lex_data, char *chunk, int file_absolute_pos, int size
case LS_pp: case LS_pp:
{ {
--pos; --pos;
int start = lex_data.token_start + 1; int start = S.token_start + 1;
c = chunk[start]; c = chunk[start];
while (start < pos && (c == ' ' || c == '\n' || c == '\t' || c == '\r' || c == '\v' || c == '\f')){ while (start < pos && (c == ' ' || c == '\n' || c == '\t' || c == '\r' || c == '\v' || c == '\f')){
@ -1080,7 +1070,7 @@ cpp_lex_nonalloc(Lex_Data lex_data, char *chunk, int file_absolute_pos, int size
String_And_Flag data = preprops.data[match.index]; String_And_Flag data = preprops.data[match.index];
token.type = (Cpp_Token_Type)data.flags; token.type = (Cpp_Token_Type)data.flags;
token.flags = CPP_TFLAG_PP_DIRECTIVE; token.flags = CPP_TFLAG_PP_DIRECTIVE;
lex_data.pp_state = (char)cpp_pp_directive_to_state(token.type); S.pp_state = (unsigned char)cpp_pp_directive_to_state(token.type);
} }
else{ else{
token.type = CPP_TOKEN_JUNK; token.type = CPP_TOKEN_JUNK;
@ -1091,7 +1081,7 @@ cpp_lex_nonalloc(Lex_Data lex_data, char *chunk, int file_absolute_pos, int size
case LS_number: case LS_number:
case LS_number0: case LS_number0:
case LS_hex: case LS_hex:
lex_data.fsm.int_state = LSINT_default; S.fsm.int_state = LSINT_default;
{ {
int done = 0; int done = 0;
@ -1105,57 +1095,57 @@ cpp_lex_nonalloc(Lex_Data lex_data, char *chunk, int file_absolute_pos, int size
++pos; ++pos;
} }
switch (lex_data.fsm.int_state){ switch (S.fsm.int_state){
case LSINT_default: case LSINT_default:
switch (c){ switch (c){
case 'u': case 'U': lex_data.fsm.int_state = LSINT_u; break; case 'u': case 'U': S.fsm.int_state = LSINT_u; break;
case 'l': lex_data.fsm.int_state = LSINT_l; break; case 'l': S.fsm.int_state = LSINT_l; break;
case 'L': lex_data.fsm.int_state = LSINT_L; break; case 'L': S.fsm.int_state = LSINT_L; break;
default: done = 1; break; default: done = 1; break;
} }
break; break;
case LSINT_u: case LSINT_u:
switch (c){ switch (c){
case 'l': lex_data.fsm.int_state = LSINT_ul; break; case 'l': S.fsm.int_state = LSINT_ul; break;
case 'L': lex_data.fsm.int_state = LSINT_uL; break; case 'L': S.fsm.int_state = LSINT_uL; break;
default: done = 1; break; default: done = 1; break;
} }
break; break;
case LSINT_l: case LSINT_l:
switch (c){ switch (c){
case 'l': lex_data.fsm.int_state = LSINT_ll; break; case 'l': S.fsm.int_state = LSINT_ll; break;
case 'U': case 'u': lex_data.fsm.int_state = LSINT_extra; break; case 'U': case 'u': S.fsm.int_state = LSINT_extra; break;
default: done = 1; break; default: done = 1; break;
} }
break; break;
case LSINT_L: case LSINT_L:
switch (c){ switch (c){
case 'L': lex_data.fsm.int_state = LSINT_ll; break; case 'L': S.fsm.int_state = LSINT_ll; break;
case 'U': case 'u': lex_data.fsm.int_state = LSINT_extra; break; case 'U': case 'u': S.fsm.int_state = LSINT_extra; break;
default: done = 1; break; default: done = 1; break;
} }
break; break;
case LSINT_ul: case LSINT_ul:
switch (c){ switch (c){
case 'l': lex_data.fsm.int_state = LSINT_extra; break; case 'l': S.fsm.int_state = LSINT_extra; break;
default: done = 1; break; default: done = 1; break;
} }
break; break;
case LSINT_uL: case LSINT_uL:
switch (c){ switch (c){
case 'L': lex_data.fsm.int_state = LSINT_extra; break; case 'L': S.fsm.int_state = LSINT_extra; break;
default: done = 1; break; default: done = 1; break;
} }
break; break;
case LSINT_ll: case LSINT_ll:
switch (c){ switch (c){
case 'u': case 'U': lex_data.fsm.int_state = LSINT_extra; break; case 'u': case 'U': S.fsm.int_state = LSINT_extra; break;
default: done = 1; break; default: done = 1; break;
} }
break; break;
@ -1208,23 +1198,13 @@ cpp_lex_nonalloc(Lex_Data lex_data, char *chunk, int file_absolute_pos, int size
case LS_comment: case LS_comment_block_ending: case LS_comment: case LS_comment_block_ending:
token.type = CPP_TOKEN_COMMENT; token.type = CPP_TOKEN_COMMENT;
token.flags = 0; token.flags = 0;
c = chunk[--pos]; pos_update_rule = PUR_unget_whitespace;
while (c == ' ' || c == '\n' || c == '\t' || c == '\r' || c == '\v' || c == '\f'){
--pos;
c = chunk[pos];
}
++pos;
break; break;
case LS_error_message: case LS_error_message:
token.type = CPP_TOKEN_ERROR_MESSAGE; token.type = CPP_TOKEN_ERROR_MESSAGE;
token.flags = 0; token.flags = 0;
c = chunk[--pos]; pos_update_rule = PUR_unget_whitespace;
while (c == ' ' || c == '\n' || c == '\t' || c == '\r' || c == '\v' || c == '\f'){
--pos;
c = chunk[pos];
}
++pos;
break; break;
case LS_dot: case LS_dot:
@ -1421,65 +1401,78 @@ cpp_lex_nonalloc(Lex_Data lex_data, char *chunk, int file_absolute_pos, int size
} }
break; break;
} }
switch (pos_update_rule){
case PUR_unget_whitespace:
c = chunk[--pos];
while (c == ' ' || c == '\n' || c == '\t' || c == '\r' || c == '\v' || c == '\f'){
--pos;
c = chunk[pos];
}
++pos;
break;
}
if ((token.flags & CPP_TFLAG_PP_DIRECTIVE) == 0){ if ((token.flags & CPP_TFLAG_PP_DIRECTIVE) == 0){
switch (lex_data.pp_state){ switch (S.pp_state){
case LSPP_include: case LSPP_include:
if (token.type != CPP_TOKEN_INCLUDE_FILE){ if (token.type != CPP_TOKEN_INCLUDE_FILE){
token.type = CPP_TOKEN_JUNK; token.type = CPP_TOKEN_JUNK;
} }
lex_data.pp_state = LSPP_junk; S.pp_state = LSPP_junk;
break; break;
case LSPP_macro_identifier: case LSPP_macro_identifier:
if (lex_data.fsm.state != LS_identifier){ if (S.fsm.state != LS_identifier){
token.type = CPP_TOKEN_JUNK; token.type = CPP_TOKEN_JUNK;
lex_data.pp_state = LSPP_junk; S.pp_state = LSPP_junk;
} }
else{ else{
lex_data.pp_state = LSPP_body; S.pp_state = LSPP_body;
} }
break; break;
case LSPP_identifier: case LSPP_identifier:
if (lex_data.fsm.state != LS_identifier){ if (S.fsm.state != LS_identifier){
token.type = CPP_TOKEN_JUNK; token.type = CPP_TOKEN_JUNK;
} }
lex_data.pp_state = LSPP_junk; S.pp_state = LSPP_junk;
break; break;
case LSPP_number: case LSPP_number:
if (token.type != CPP_TOKEN_INTEGER_CONSTANT){ if (token.type != CPP_TOKEN_INTEGER_CONSTANT){
token.type = CPP_TOKEN_JUNK; token.type = CPP_TOKEN_JUNK;
lex_data.pp_state = LSPP_junk; S.pp_state = LSPP_junk;
} }
else{ else{
lex_data.pp_state = LSPP_include; S.pp_state = LSPP_include;
} }
break; break;
case LSPP_junk: case LSPP_junk:
token.type = CPP_TOKEN_JUNK; token.type = CPP_TOKEN_JUNK;
break; break;
} }
} }
if (lex_data.fsm.emit_token){ if (S.fsm.emit_token){
token.start = lex_data.token_start; token.start = S.token_start;
token.size = pos - lex_data.token_start; token.size = pos - S.token_start;
token.flags |= (lex_data.fsm.multi_line)?(CPP_TFLAG_MULTILINE):(0); token.flags |= (S.fsm.multi_line)?(CPP_TFLAG_MULTILINE):(0);
token.flags |= (lex_data.pp_state != LSPP_default)?(CPP_TFLAG_PP_BODY):(0); if ((token.flags & CPP_TFLAG_PP_DIRECTIVE) == 0){
token.state_flags = lex_data.pp_state; token.flags |= (S.pp_state != LSPP_default)?(CPP_TFLAG_PP_BODY):(0);
}
token.state_flags = S.pp_state;
cpp_push_token_nonalloc(out_tokens, &token_i, token); cpp_push_token_nonalloc(out_tokens, &token_i, token);
} }
} }
} }
token_stack_out->count = token_i; token_stack_out->count = token_i;
if (pos == end_pos) lex_data.completed = 1; if (pos == end_pos) S.completed = 1;
return(lex_data); return(S);
} }
} }

View File

@ -42,6 +42,7 @@ dump_file(char *filename){
data.data = (byte*)malloc(data.size + 1); data.data = (byte*)malloc(data.size + 1);
ReadFile(file, data.data, lo, &lo, 0); ReadFile(file, data.data, lo, &lo, 0);
data.data[data.size] = 0;
assert((int)lo == data.size); assert((int)lo == data.size);
@ -180,9 +181,24 @@ struct Experiment{
Cpp_Token_Stack testing_stack; Cpp_Token_Stack testing_stack;
int passed_total, test_total; int passed_total, test_total;
}; };
int
passed(Experiment exp){
return (exp.passed_total == exp.test_total && exp.passed_total > 1);
}
i64 handcoded_lexer_time = 0; struct Times{
i64 fsm_lexer_time = 0; i64 handcoded;
i64 fsm;
};
Times time;
void
begin_t(Times *t){
time = *t;
}
void
end_t(Times *t){
*t = time;
}
static void static void
run_experiment(Experiment *exp, char *filename, int verbose, int chunks){ run_experiment(Experiment *exp, char *filename, int verbose, int chunks){
@ -216,25 +232,26 @@ run_experiment(Experiment *exp, char *filename, int verbose, int chunks){
start = __rdtsc(); start = __rdtsc();
cpp_lex_file_nonalloc(file_cpp, &exp->correct_stack, lex_data); cpp_lex_file_nonalloc(file_cpp, &exp->correct_stack, lex_data);
handcoded_lexer_time += (__rdtsc() - start); time.handcoded += (__rdtsc() - start);
start = __rdtsc(); start = __rdtsc();
if (chunks){ if (chunks){
int relevant_size = file_data.size + 1;
is_last = 0; is_last = 0;
for (k = 0; k < file_data.size; k += chunks){ for (k = 0; k < relevant_size; k += chunks){
chunk_size = chunks; chunk_size = chunks;
if (chunk_size + k >= file_data.size){ if (chunk_size + k >= relevant_size){
chunk_size = file_data.size - k; chunk_size = relevant_size - k;
is_last = 1; is_last = 1;
} }
ld = new_lex::cpp_lex_nonalloc(ld, (char*)file_data.data + k, k, is_last, chunk_size, &exp->testing_stack); ld = new_lex::cpp_lex_nonalloc(ld, (char*)file_data.data + k, k, chunk_size, &exp->testing_stack);
} }
} }
else{ else{
new_lex::cpp_lex_nonalloc(ld, (char*)file_data.data, 0, file_data.size, 1, &exp->testing_stack); new_lex::cpp_lex_nonalloc(ld, (char*)file_data.data, 0, file_data.size, &exp->testing_stack);
} }
fsm_lexer_time += (__rdtsc() - start); time.fsm += (__rdtsc() - start);
} }
if (exp->correct_stack.count != exp->testing_stack.count){ if (exp->correct_stack.count != exp->testing_stack.count){
@ -290,58 +307,96 @@ run_experiment(Experiment *exp, char *filename, int verbose, int chunks){
} }
} }
#define OUTLINE(type) "%-30s "type"\n"
#define OUTLINE_VAR(t, var) #var, (t)var
void
show_time(Times t, int repeats, char *type){
f32 speed_up = ((f32)t.handcoded) / t.fsm;
printf(
"\n%s time for %d repeates\n"
OUTLINE("%d")
OUTLINE("%d")
OUTLINE("%f"),
type,
repeats,
OUTLINE_VAR(i32, t.handcoded),
OUTLINE_VAR(i32, t.fsm),
OUTLINE_VAR(f32, speed_up)
);
}
#define BASE_DIR "w:/4ed/data/test/" #define BASE_DIR "w:/4ed/data/test/"
int main(){ int main(){
int repeats = 100; int repeats = 1;
int verbose_level = -1; int verbose_level = 0;
int chunks = 0; int chunks = 64;
char test_directory[] = BASE_DIR; char test_directory[] = BASE_DIR;
File_List all_files = {}; File_List all_files = {};
Experiment exp = {}; Experiment exp = {};
Experiment chunk_exp = {};
Times exp_t = {};
Times chunk_exp_t = {};
init_test_stack(&exp.correct_stack); init_test_stack(&exp.correct_stack);
init_test_stack(&exp.testing_stack); init_test_stack(&exp.testing_stack);
init_test_stack(&chunk_exp.correct_stack);
init_test_stack(&chunk_exp.testing_stack);
AllowLocal(test_directory); AllowLocal(test_directory);
AllowLocal(all_files); AllowLocal(all_files);
#if 0 #if 1
(void)(repeats); (void)(repeats);
(void)(verbose_level); (void)(verbose_level);
run_experiment(&exp, BASE_DIR "crazywords.cpp", 1, chunks); #define TEST_FILE "crazywords.cpp"
if (chunks){
begin_t(&chunk_exp_t);
printf("With chunks of %d\n", chunks);
run_experiment(&chunk_exp, BASE_DIR TEST_FILE, 1, chunks);
end_t(&chunk_exp_t);
}
begin_t(&exp_t);
printf("Unchunked\n");
run_experiment(&exp, BASE_DIR TEST_FILE, 1, 0);
end_t(&exp_t);
#else #else
system_set_file_list(&all_files, make_lit_string(test_directory)); system_set_file_list(&all_files, make_lit_string(test_directory));
for (int j = 0; j < repeats; ++j){ for (int j = 0; j < repeats; ++j){
for (int i = 0; i < all_files.count; ++i){ for (int i = 0; i < all_files.count; ++i){
if (all_files.infos[i].folder == 0){ if (all_files.infos[i].folder == 0){
run_experiment(&exp, all_files.infos[i].filename.str, verbose_level, chunks); if (chunks){
begin_t(&chunk_exp_t);
run_experiment(&chunk_exp, all_files.infos[i].filename.str, verbose_level, chunks);
end_t(&chunk_exp_t);
}
begin_t(&exp_t);
run_experiment(&exp, all_files.infos[i].filename.str, verbose_level, 0);
end_t(&exp_t);
} }
} }
} }
#endif #endif
printf("you passed %d / %d tests\n", exp.passed_total, exp.test_total); if (chunks){
printf("chunked passed %d / %d tests\n", chunk_exp.passed_total, chunk_exp.test_total);
#define OUTLINE(type) "%-30s "type"\n" }
#define OUTLINE_VAR(t, var) #var, (t)var
if (exp.passed_total == exp.test_total && exp.passed_total > 1){ printf("unchunk passed %d / %d tests\n", exp.passed_total, exp.test_total);
f32 speed_up = ((f32)handcoded_lexer_time) / fsm_lexer_time;
printf(
"\nTime information for %d repeates\n"
OUTLINE("%d")
OUTLINE("%d")
OUTLINE("%f"),
repeats, if (passed(exp) && (chunks == 0 || passed(chunk_exp))){
OUTLINE_VAR(i32, handcoded_lexer_time), if (chunks){
OUTLINE_VAR(i32, fsm_lexer_time), show_time(chunk_exp_t, repeats, "Chunked");
OUTLINE_VAR(f32, speed_up) }
); show_time(exp_t, repeats, "Unchunked");
} }
return(0); return(0);