ReactOS 0.4.15-dev-6694-g4ba8af9
asmpp.cpp File Reference
#include "tokenizer.hpp"
#include <cstdlib>
#include <cstdio>
#include <sstream>
#include <ctime>
Include dependency graph for asmpp.cpp:

Go to the source code of this file.

Macros

#define PROFILING_ENABLED   0
 
#define FOLLOWED_BY(x)   x
 
#define ANY_CHAR   R"((?:.|\n))"
 
#define WHITESPACE   R"((?:[ \t]++))"
 
#define NEWLINE   R"([\n])"
 
#define WS_OR_NL   R"((?:)" WHITESPACE "|" NEWLINE R"()+)"
 
#define SEPARATOR   R"([\s,\=\+\-\*\/\:\~\[\]])"
 
#define INSTRUCTION
 

Enumerations

enum  TOKEN_TYPE {
  Invalid = -1 , Eof , WhiteSpace , NewLine ,
  Comment , DecNumber , HexNumber , String ,
  BraceOpen , BraceClose , MemRefStart , MemRefEnd ,
  Colon , Operator , StringDef , KW_include ,
  KW_const , KW_code , KW_endprolog , KW_ALIGN ,
  KW_EXTERN , KW_PUBLIC , KW_ENDM , KW_END ,
  KW_if , KW_ifdef , KW_ifndef , KW_else ,
  KW_endif , KW_allocstack , KW_savereg , KW_savexmm128 ,
  KW_DB , KW_DW , KW_DD , KW_DQ ,
  KW_EQU , KW_TEXTEQU , KW_MACRO , KW_PROC ,
  KW_FRAME , KW_ENDP , KW_RECORD , KW_MASK ,
  KW_ERRDEF , Filename , Instruction , Reg8 ,
  Reg16 , Reg32 , Reg64 , RegXmm ,
  BYTE_PTR , WORD_PTR , DWORD_PTR , QWORD_PTR ,
  XMMWORD_PTR , LabelName , Identifier
}
 

Functions

int fake_printf (const char *format,...)
 
void add_mem_id (Token &tok)
 
bool is_mem_id (Token &tok)
 
bool iequals (const string &a, const string &b)
 
Token get_expected_token (Token &&tok, TOKEN_TYPE type)
 
Token get_ws (Token &&tok)
 
Token get_ws_or_nl (Token &&tok)
 
bool is_string_in_list (vector< string > list, string str)
 
size_t translate_token (TokenList &tokens, size_t index, const vector< string > &macro_params)
 
size_t complete_line (TokenList &tokens, size_t index, const vector< string > &macro_params)
 
size_t translate_expression (TokenList &tokens, size_t index, const vector< string > &macro_params)
 
size_t translate_mem_ref (TokenList &tokens, size_t index, const vector< string > &macro_params)
 
size_t translate_instruction_param (TokenList &tokens, size_t index, const vector< string > &macro_params)
 
size_t translate_instruction (TokenList &tokens, size_t index, const vector< string > &macro_params)
 
size_t translate_item (TokenList &tokens, size_t index, const vector< string > &macro_params)
 
size_t translate_list (TokenList &tokens, size_t index, const vector< string > &macro_params)
 
size_t translate_data_def (TokenList &tokens, size_t index, const vector< string > &macro_params)
 
size_t translate_construct_one_param (string translated, TokenList &tokens, size_t index, const vector< string > &macro_params)
 
size_t translate_record (TokenList &tokens, size_t index, const vector< string > &macro_params)
 
size_t translate_identifier_construct (TokenList &tokens, size_t index, const vector< string > &macro_params)
 
size_t translate_construct (TokenList &tokens, size_t index, const vector< string > &macro_params)
 
size_t translate_macro (TokenList &tokens, size_t index)
 
void translate (TokenList &tokens)
 
int main (int argc, char *argv[])
 

Variables

time_t search_time
 
vector< TOKEN_DEFg_TokenList
 
unsigned int g_label_number = 0
 
vector< stringg_identifiers
 

Macro Definition Documentation

◆ ANY_CHAR

#define ANY_CHAR   R"((?:.|\n))"

Definition at line 109 of file asmpp.cpp.

◆ FOLLOWED_BY

#define FOLLOWED_BY (   x)    x

Definition at line 107 of file asmpp.cpp.

◆ INSTRUCTION

#define INSTRUCTION

Definition at line 115 of file asmpp.cpp.

◆ NEWLINE

#define NEWLINE   R"([\n])"

Definition at line 111 of file asmpp.cpp.

◆ PROFILING_ENABLED

#define PROFILING_ENABLED   0

Definition at line 22 of file asmpp.cpp.

◆ SEPARATOR

#define SEPARATOR   R"([\s,\=\+\-\*\/\:\~\[\]])"

Definition at line 113 of file asmpp.cpp.

◆ WHITESPACE

#define WHITESPACE   R"((?:[ \t]++))"

Definition at line 110 of file asmpp.cpp.

◆ WS_OR_NL

#define WS_OR_NL   R"((?:)" WHITESPACE "|" NEWLINE R"()+)"

Definition at line 112 of file asmpp.cpp.

Enumeration Type Documentation

◆ TOKEN_TYPE

Enumerator
Invalid 
Eof 
WhiteSpace 
NewLine 
Comment 
DecNumber 
HexNumber 
String 
BraceOpen 
BraceClose 
MemRefStart 
MemRefEnd 
Colon 
Operator 
StringDef 
KW_include 
KW_const 
KW_code 
KW_endprolog 
KW_ALIGN 
KW_EXTERN 
KW_PUBLIC 
KW_ENDM 
KW_END 
KW_if 
KW_ifdef 
KW_ifndef 
KW_else 
KW_endif 
KW_allocstack 
KW_savereg 
KW_savexmm128 
KW_DB 
KW_DW 
KW_DD 
KW_DQ 
KW_EQU 
KW_TEXTEQU 
KW_MACRO 
KW_PROC 
KW_FRAME 
KW_ENDP 
KW_RECORD 
KW_MASK 
KW_ERRDEF 
Filename 
Instruction 
Reg8 
Reg16 
Reg32 
Reg64 
RegXmm 
BYTE_PTR 
WORD_PTR 
DWORD_PTR 
QWORD_PTR 
XMMWORD_PTR 
LabelName 
Identifier 

Definition at line 28 of file asmpp.cpp.

29{
30 Invalid = -1,
31 Eof,
33 NewLine,
34 Comment,
37 String,
38
43 Colon,
46
49 KW_code,
54 KW_ENDM,
55 KW_END,
56 KW_if,
59 KW_else,
61
65
66 KW_DB,
67 KW_DW,
68 KW_DD,
69 KW_DQ,
70 KW_EQU,
73 KW_PROC,
75 KW_ENDP,
77
78 KW_MASK,
80
83 Reg8,
84 Reg16,
85 Reg32,
86 Reg64,
87 RegXmm,
93
96};
@ KW_allocstack
Definition: asmpp.cpp:62
@ Reg32
Definition: asmpp.cpp:85
@ KW_FRAME
Definition: asmpp.cpp:74
@ KW_savereg
Definition: asmpp.cpp:63
@ KW_EXTERN
Definition: asmpp.cpp:52
@ KW_PROC
Definition: asmpp.cpp:73
@ StringDef
Definition: asmpp.cpp:45
@ MemRefEnd
Definition: asmpp.cpp:42
@ KW_ENDM
Definition: asmpp.cpp:54
@ BraceClose
Definition: asmpp.cpp:40
@ Comment
Definition: asmpp.cpp:34
@ KW_else
Definition: asmpp.cpp:59
@ Instruction
Definition: asmpp.cpp:82
@ KW_END
Definition: asmpp.cpp:55
@ KW_code
Definition: asmpp.cpp:49
@ KW_endprolog
Definition: asmpp.cpp:50
@ DecNumber
Definition: asmpp.cpp:35
@ Identifier
Definition: asmpp.cpp:95
@ KW_DW
Definition: asmpp.cpp:67
@ BraceOpen
Definition: asmpp.cpp:39
@ KW_RECORD
Definition: asmpp.cpp:76
@ Colon
Definition: asmpp.cpp:43
@ KW_ENDP
Definition: asmpp.cpp:75
@ Filename
Definition: asmpp.cpp:81
@ KW_EQU
Definition: asmpp.cpp:70
@ BYTE_PTR
Definition: asmpp.cpp:88
@ RegXmm
Definition: asmpp.cpp:87
@ WhiteSpace
Definition: asmpp.cpp:32
@ KW_DB
Definition: asmpp.cpp:66
@ MemRefStart
Definition: asmpp.cpp:41
@ QWORD_PTR
Definition: asmpp.cpp:91
@ KW_include
Definition: asmpp.cpp:47
@ KW_MACRO
Definition: asmpp.cpp:72
@ HexNumber
Definition: asmpp.cpp:36
@ KW_DD
Definition: asmpp.cpp:68
@ LabelName
Definition: asmpp.cpp:94
@ KW_const
Definition: asmpp.cpp:48
@ KW_TEXTEQU
Definition: asmpp.cpp:71
@ Reg8
Definition: asmpp.cpp:83
@ NewLine
Definition: asmpp.cpp:33
@ KW_ifndef
Definition: asmpp.cpp:58
@ KW_ifdef
Definition: asmpp.cpp:57
@ XMMWORD_PTR
Definition: asmpp.cpp:92
@ KW_PUBLIC
Definition: asmpp.cpp:53
@ Operator
Definition: asmpp.cpp:44
@ KW_DQ
Definition: asmpp.cpp:69
@ Eof
Definition: asmpp.cpp:31
@ KW_ALIGN
Definition: asmpp.cpp:51
@ KW_savexmm128
Definition: asmpp.cpp:64
@ DWORD_PTR
Definition: asmpp.cpp:90
@ String
Definition: asmpp.cpp:37
@ KW_endif
Definition: asmpp.cpp:60
@ KW_MASK
Definition: asmpp.cpp:78
@ KW_if
Definition: asmpp.cpp:56
@ WORD_PTR
Definition: asmpp.cpp:89
@ Invalid
Definition: asmpp.cpp:30
@ Reg16
Definition: asmpp.cpp:84
@ Reg64
Definition: asmpp.cpp:86
@ KW_ERRDEF
Definition: asmpp.cpp:79

Function Documentation

◆ add_mem_id()

void add_mem_id ( Token tok)

Definition at line 302 of file asmpp.cpp.

303{
304 g_identifiers.push_back(tok.str());
305 //fprintf(stderr, "Added mem id: '%s'\n", tok.str().c_str());
306}
vector< string > g_identifiers
Definition: asmpp.cpp:299
std::string str() const
Definition: tokenizer.hpp:50

Referenced by translate_construct(), and translate_identifier_construct().

◆ complete_line()

size_t complete_line ( TokenList tokens,
size_t  index,
const vector< string > &  macro_params 
)

Definition at line 442 of file asmpp.cpp.

443{
444 while (index < tokens.size())
445 {
446 Token tok = tokens[index];
447 index = translate_token(tokens, index, macro_params);
448 if ((tok.type() == TOKEN_TYPE::NewLine) ||
449 (tok.type() == TOKEN_TYPE::Comment))
450 {
451 break;
452 }
453 }
454
455 return index;
456}
size_t translate_token(TokenList &tokens, size_t index, const vector< string > &macro_params)
Definition: asmpp.cpp:382
#define index(s, c)
Definition: various.h:29
size_t size() const
Definition: tokenizer.hpp:239
int type() const
Definition: tokenizer.hpp:55
GLuint index
Definition: glext.h:6031

Referenced by translate_construct().

◆ fake_printf()

int fake_printf ( const char format,
  ... 
)

Definition at line 98 of file asmpp.cpp.

99{
100 return 0;
101}

◆ get_expected_token()

Token get_expected_token ( Token &&  tok,
TOKEN_TYPE  type 
)

Definition at line 335 of file asmpp.cpp.

336{
337 if (tok.type() != type)
338 {
339 throw "Not white space after identifier!\n";
340 }
341
342 return tok;
343}
GLuint GLuint GLsizei GLenum type
Definition: gl.h:1545

Referenced by translate_construct(), translate_identifier_construct(), and translate_record().

◆ get_ws()

Token get_ws ( Token &&  tok)

Definition at line 345 of file asmpp.cpp.

346{
347 int type = tok.type();
348 if (type != TOKEN_TYPE::WhiteSpace)
349 {
350 throw "Not white space after identifier!\n";
351 }
352
353 return tok;
354}

Referenced by translate_construct_one_param(), translate_data_def(), and translate_identifier_construct().

◆ get_ws_or_nl()

Token get_ws_or_nl ( Token &&  tok)

Definition at line 356 of file asmpp.cpp.

357{
358 int type = tok.type();
359 if ((type != TOKEN_TYPE::WhiteSpace) &&
360 (type != TOKEN_TYPE::NewLine))
361 {
362 throw "Not white space after identifier!\n";
363 }
364
365 return tok;
366}

Referenced by translate_construct().

◆ iequals()

bool iequals ( const string a,
const string b 
)

Definition at line 323 of file asmpp.cpp.

324{
325 size_t sz = a.size();
326 if (b.size() != sz)
327 return false;
328 for (unsigned int i = 0; i < sz; ++i)
329 if (tolower(a[i]) != tolower(b[i]))
330 return false;
331 return true;
332}
int tolower(int c)
Definition: utclib.c:902
GLboolean GLboolean GLboolean b
Definition: glext.h:6204
GLboolean GLboolean GLboolean GLboolean a
Definition: glext.h:6204
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248

Referenced by translate_expression().

◆ is_mem_id()

bool is_mem_id ( Token tok)

Definition at line 309 of file asmpp.cpp.

310{
311 for (auto id : g_identifiers)
312 {
313 if (id == tok.str())
314 {
315 return true;
316 }
317 }
318
319 return false;
320}

Referenced by translate_instruction_param().

◆ is_string_in_list()

bool is_string_in_list ( vector< string list,
string  str 
)

Definition at line 368 of file asmpp.cpp.

369{
370 for (string &s : list)
371 {
372 if (s == str)
373 {
374 return true;
375 }
376 }
377
378 return false;
379}
Definition: list.h:37
GLdouble s
Definition: gl.h:2039
const WCHAR * str

Referenced by translate_token().

◆ main()

int main ( int argc  ,
char argv[] 
)

Definition at line 1164 of file asmpp.cpp.

1165{
1166 if (argc < 2)
1167 {
1168 fprintf(stderr, "Invalid parameter!\n");
1169 return -1;
1170 }
1171
1172#if PROFILING_ENABLED
1173 time_t start_time = time(NULL);
1174#endif
1175
1176 try
1177 {
1178 // Open and read the input file
1179 string filename(argv[1]);
1182 buffer << file.rdbuf();
1183 string text = buffer.str();
1184
1185 // Create the tokenizer
1186 Tokenizer tokenizer(g_TokenList);
1187
1188 // Get a token list
1189 TokenList toklist(tokenizer, text);
1190
1191 // Now translate the tokens
1192 translate(toklist);
1193 }
1194 catch (const char* message)
1195 {
1196 fprintf(stderr, "Exception caught: '%s'\n", message);
1197 return -2;
1198 }
1199
1200#if PROFILING_ENABLED
1201 time_t total_time = time(NULL) + 1 - start_time;
1202 fprintf(stderr, "total_time = %llu\n", total_time);
1203 fprintf(stderr, "search_time = %llu\n", search_time);
1204 fprintf(stderr, "search: %llu %%\n", search_time * 100 / total_time);
1205#endif
1206
1207 return 0;
1208}
static int argc
Definition: ServiceArgs.c:12
vector< TOKEN_DEF > g_TokenList
Definition: asmpp.cpp:225
void translate(TokenList &tokens)
Definition: asmpp.cpp:1142
time_t search_time
Definition: asmpp.cpp:26
#define NULL
Definition: types.h:112
const WCHAR * text
Definition: package.c:1799
__kernel_time_t time_t
Definition: linux.h:252
GLuint buffer
Definition: glext.h:5915
#define stderr
Definition: stdio.h:100
_Check_return_opt_ _CRTIMP int __cdecl fprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format,...)
const char * filename
Definition: ioapi.h:137
__u16 time
Definition: mkdosfs.c:8
#define argv
Definition: mplay32.c:18
Definition: fci.c:127
Definition: tftpd.h:60

◆ translate()

void translate ( TokenList tokens)

Definition at line 1142 of file asmpp.cpp.

1143{
1144 size_t index = 0;
1145 size_t size = tokens.size();
1146 vector<string> empty_macro_params;
1147
1148 while (index < size)
1149 {
1150 // Macros are special
1151 if ((tokens[index].type() == TOKEN_TYPE::Identifier) &&
1152 (tokens[index + 1].type() == TOKEN_TYPE::WhiteSpace) &&
1153 (tokens[index + 2].type() == TOKEN_TYPE::KW_MACRO))
1154 {
1155 index = translate_macro(tokens, index);
1156 }
1157 else
1158 {
1159 index = translate_construct(tokens, index, empty_macro_params);
1160 }
1161 }
1162}
size_t translate_construct(TokenList &tokens, size_t index, const vector< string > &macro_params)
Definition: asmpp.cpp:967
size_t translate_macro(TokenList &tokens, size_t index)
Definition: asmpp.cpp:1089
GLsizeiptr size
Definition: glext.h:5919

Referenced by main().

◆ translate_construct()

size_t translate_construct ( TokenList tokens,
size_t  index,
const vector< string > &  macro_params 
)

Definition at line 967 of file asmpp.cpp.

968{
969 Token tok = tokens[index];
970
971 switch (tok.type())
972 {
973 case TOKEN_TYPE::WhiteSpace:
974 case TOKEN_TYPE::NewLine:
975 case TOKEN_TYPE::Comment:
976 return translate_token(tokens, index, macro_params);
977
978 case TOKEN_TYPE::Identifier:
979 return translate_identifier_construct(tokens, index, macro_params);
980
981 case TOKEN_TYPE::KW_ALIGN:
982 index = translate_construct_one_param(".align", tokens, index, macro_params);
983 break;
984
985 case TOKEN_TYPE::KW_allocstack:
986 index = translate_construct_one_param(".seh_stackalloc", tokens, index, macro_params);
987 break;
988
989 case TOKEN_TYPE::KW_code:
990#ifdef TARGET_amd64
991 printf(".code64");
992#else
993 printf(".code");
994#endif
995 printf(" .intel_syntax noprefix");
996 index++;
997 break;
998
999 case TOKEN_TYPE::KW_const:
1000 printf(".section .rdata");
1001 index++;
1002 break;
1003
1004 case TOKEN_TYPE::KW_DB:
1005 case TOKEN_TYPE::KW_DW:
1006 case TOKEN_TYPE::KW_DD:
1007 case TOKEN_TYPE::KW_DQ:
1008 return translate_data_def(tokens, index, macro_params);
1009
1010 case TOKEN_TYPE::KW_END:
1011 printf("// END\n");
1012 return tokens.size();
1013
1014 case TOKEN_TYPE::KW_endprolog:
1015 printf(".seh_endprologue");
1016 index++;
1017 break;
1018
1019 case TOKEN_TYPE::KW_EXTERN:
1020 {
1021 Token tok1 = get_ws_or_nl(tokens[index + 1]);
1022 Token tok2 = get_expected_token(tokens[index + 2], TOKEN_TYPE::Identifier);
1023 add_mem_id(tok2);
1024 printf("//");
1025 return complete_line(tokens, index, macro_params);
1026 }
1027
1028 case TOKEN_TYPE::KW_if:
1029 case TOKEN_TYPE::KW_ifdef:
1030 case TOKEN_TYPE::KW_ifndef:
1031 case TOKEN_TYPE::KW_else:
1032 case TOKEN_TYPE::KW_endif:
1033 // TODO: handle parameter differences between "if" and ".if" etc.
1034 printf(".");
1035 return complete_line(tokens, index, macro_params);
1036
1037 case TOKEN_TYPE::KW_include:
1038 {
1039 // The next token should be white space
1040 Token tok1 = get_ws_or_nl(tokens[index + 1]);
1041 Token tok2 = get_expected_token(tokens[index + 2], TOKEN_TYPE::Filename);
1042 printf("#include \"%s.h\"", tok2.str().c_str());
1043 index += 3;
1044 break;
1045 }
1046
1047 case TOKEN_TYPE::KW_PUBLIC:
1048 index = translate_construct_one_param(".global", tokens, index, macro_params);
1049 break;
1050
1051 case TOKEN_TYPE::KW_savereg:
1052 printf(".seh_savereg");
1053 return complete_line(tokens, index + 1, macro_params);
1054
1055 case TOKEN_TYPE::KW_savexmm128:
1056 printf(".seh_savexmm");
1057 return complete_line(tokens, index + 1, macro_params);
1058
1059 case TOKEN_TYPE::Instruction:
1060 index = translate_instruction(tokens, index, macro_params);
1061 break;
1062
1063 case TOKEN_TYPE::KW_ERRDEF:
1064 printf("//");
1065 return complete_line(tokens, index, macro_params);
1066
1067 default:
1068 throw "failed to translate construct";
1069 }
1070
1071 // Skip optional white-space
1072 if (tokens[index].type() == TOKEN_TYPE::WhiteSpace)
1073 {
1074 index++;
1075 }
1076
1077 // Line should end here!
1078 Token end = tokens[index];
1079 if ((end.type() != TOKEN_TYPE::Comment) &&
1080 (end.type() != TOKEN_TYPE::NewLine))
1081 {
1082 throw "unexpected tokens";
1083 }
1084
1085 return index;
1086}
void add_mem_id(Token &tok)
Definition: asmpp.cpp:302
size_t translate_data_def(TokenList &tokens, size_t index, const vector< string > &macro_params)
Definition: asmpp.cpp:697
Token get_ws_or_nl(Token &&tok)
Definition: asmpp.cpp:356
size_t complete_line(TokenList &tokens, size_t index, const vector< string > &macro_params)
Definition: asmpp.cpp:442
Token get_expected_token(Token &&tok, TOKEN_TYPE type)
Definition: asmpp.cpp:335
size_t translate_identifier_construct(TokenList &tokens, size_t index, const vector< string > &macro_params)
Definition: asmpp.cpp:878
size_t translate_construct_one_param(string translated, TokenList &tokens, size_t index, const vector< string > &macro_params)
Definition: asmpp.cpp:788
size_t translate_instruction(TokenList &tokens, size_t index, const vector< string > &macro_params)
Definition: asmpp.cpp:607
#define printf
Definition: freeldr.h:94
GLuint GLuint end
Definition: gl.h:1545

Referenced by translate(), and translate_macro().

◆ translate_construct_one_param()

size_t translate_construct_one_param ( string  translated,
TokenList tokens,
size_t  index,
const vector< string > &  macro_params 
)

Definition at line 788 of file asmpp.cpp.

789{
790 // The next token should be white space
791 Token tok1 = get_ws(tokens[index + 1]);
792
793 printf("%s%s", translated.c_str(), tok1.str().c_str());
794 return translate_expression(tokens, index + 2, macro_params);
795}
size_t translate_expression(TokenList &tokens, size_t index, const vector< string > &macro_params)
Definition: asmpp.cpp:459
Token get_ws(Token &&tok)
Definition: asmpp.cpp:345
const _CharT * c_str() const
Definition: _string.h:949

Referenced by translate_construct().

◆ translate_data_def()

size_t translate_data_def ( TokenList tokens,
size_t  index,
const vector< string > &  macro_params 
)

Definition at line 697 of file asmpp.cpp.

698{
699 Token tok = tokens[index];
700 Token tok1 = get_ws(tokens[index + 1]);
701 string directive, need, have ="";
702
703 switch (tok.type())
704 {
705 case TOKEN_TYPE::KW_DB:
706 directive = ".byte";
707 break;
708
709 case TOKEN_TYPE::KW_DW:
710 directive = ".short";
711 break;
712
713 case TOKEN_TYPE::KW_DD:
714 directive = ".long";
715 break;
716
717 case TOKEN_TYPE::KW_DQ:
718 directive = ".quad";
719 break;
720 }
721
722 index += 2;
723
724 while (index < tokens.size())
725 {
726 // Check if we need '.ascii' for ASCII strings
727 if (tokens[index].str()[0] == '\"')
728 {
729 need = ".ascii";
730 }
731 else
732 {
733 need = directive;
734 }
735
736 // Output the directive we need (or a comma)
737 if (have == "")
738 {
739 printf("%s ", need.c_str());
740 }
741 else if (have != need)
742 {
743 printf("\n%s ", need.c_str());
744 }
745 else
746 {
747 printf(", ");
748 }
749
750 have = need;
751
752 // The item itself
753 index = translate_item(tokens, index, macro_params);
754
755 // Optional white space
756 if (tokens[index].type() == TOKEN_TYPE::WhiteSpace)
757 {
758 index = translate_token(tokens, index, macro_params);
759 }
760
761 // End of list?
762 if ((tokens[index].type() == TOKEN_TYPE::Comment) ||
763 (tokens[index].type() == TOKEN_TYPE::NewLine))
764 {
765 return index;
766 }
767
768 // We expect a comma here
769 if ((tokens[index].type() != TOKEN_TYPE::Operator) ||
770 (tokens[index].str() != ","))
771 {
772 throw "Unexpected end of list";
773 }
774
775 // Skip comma and optional white-space
776 index++;
777 if (tokens[index].type() == TOKEN_TYPE::WhiteSpace)
778 {
779 index++;
780 }
781 }
782
783 throw "Failed to translate list";
784 return -1;
785}
size_t translate_item(TokenList &tokens, size_t index, const vector< string > &macro_params)
Definition: asmpp.cpp:643
static UINT PSTR DWORD UINT * need
Definition: parser.c:36

Referenced by translate_construct(), and translate_identifier_construct().

◆ translate_expression()

size_t translate_expression ( TokenList tokens,
size_t  index,
const vector< string > &  macro_params 
)

Definition at line 459 of file asmpp.cpp.

460{
461 while (index < tokens.size())
462 {
463 Token tok = tokens[index];
464 switch (tok.type())
465 {
466 case TOKEN_TYPE::NewLine:
467 case TOKEN_TYPE::Comment:
468 return index;
469
470 case TOKEN_TYPE::KW_MASK:
471 printf("MASK_");
472 index += 2;
473 break;
474
475 case TOKEN_TYPE::Instruction:
476 if (iequals(tok.str(), "and"))
477 {
478 printf("&");
479 index += 1;
480 }
481 else if (iequals(tok.str(), "or"))
482 {
483 printf("|");
484 index += 1;
485 }
486 else if (iequals(tok.str(), "shl"))
487 {
488 printf("<<");
489 index += 1;
490 }
491 else if (iequals(tok.str(), "not"))
492 {
493 printf("!");
494 index += 1;
495 }
496 else
497 {
498 throw "Invalid expression";
499 }
500 break;
501
502 case TOKEN_TYPE::Operator:
503 if (tok.str() == ",")
504 {
505 return index;
506 }
507 case TOKEN_TYPE::WhiteSpace:
508 case TOKEN_TYPE::BraceOpen:
509 case TOKEN_TYPE::BraceClose:
510 case TOKEN_TYPE::DecNumber:
511 case TOKEN_TYPE::HexNumber:
512 case TOKEN_TYPE::Identifier:
513 index = translate_token(tokens, index, macro_params);
514 break;
515
516 default:
517 index = translate_token(tokens, index, macro_params);
518 }
519 }
520
521 return index;
522}
bool iequals(const string &a, const string &b)
Definition: asmpp.cpp:323

Referenced by translate_construct_one_param(), translate_identifier_construct(), and translate_instruction_param().

◆ translate_identifier_construct()

size_t translate_identifier_construct ( TokenList tokens,
size_t  index,
const vector< string > &  macro_params 
)

Definition at line 878 of file asmpp.cpp.

879{
880 Token tok = tokens[index];
881 Token tok1 = tokens[index + 1];
882
883 if (tok1.type() == TOKEN_TYPE::Colon)
884 {
885 if (tok.str() == "@@")
886 {
888 printf("%u:", g_label_number);
889 }
890 else
891 {
892 printf("%s:", tok.str().c_str());
893 }
894 return index + 2;
895 }
896
897 Token tok2 = tokens[index + 2];
898
899 switch (tok2.type())
900 {
901 case TOKEN_TYPE::KW_MACRO:
902 throw "Cannot have a nested macro!";
903
904 case TOKEN_TYPE::KW_DB:
905 case TOKEN_TYPE::KW_DW:
906 case TOKEN_TYPE::KW_DD:
907 case TOKEN_TYPE::KW_DQ:
908 printf("%s:%s", tok.str().c_str(), tok1.str().c_str());
909 add_mem_id(tok);
910 return translate_data_def(tokens, index + 2, macro_params);
911
912 case TOKEN_TYPE::KW_EQU:
913 //printf("%s%s", tok.str().c_str(), tok1.str().c_str());
914 printf("#define %s ", tok.str().c_str());
915 return translate_expression(tokens, index + 3, macro_params);
916
917 case TOKEN_TYPE::KW_TEXTEQU:
918 {
919 Token tok3 = get_ws(tokens[index + 3]);
920 Token tok4 = get_expected_token(tokens[index + 4], TOKEN_TYPE::StringDef);
921
922 string textdef = tok4.str();
923 printf("#define %s %s", tok.str().c_str(), textdef.substr(1, textdef.size() - 2).c_str());
924 return index + 5;
925 }
926
927 case TOKEN_TYPE::KW_PROC:
928 {
929 printf(".func %s\n", tok.str().c_str());
930 printf("%s:", tok.str().c_str());
931 index += 3;
932
933 if ((tokens[index].type() == TOKEN_TYPE::WhiteSpace) &&
934 (tokens[index + 1].type() == TOKEN_TYPE::KW_FRAME))
935 {
936#ifdef TARGET_amd64
937 printf("\n.seh_proc %s\n", tok.str().c_str());
938#else
939 printf("\n.cfi_startproc\n");
940#endif
941 index += 2;
942 }
943 break;
944 }
945
946 case TOKEN_TYPE::KW_ENDP:
947 {
948 printf(".seh_endproc\n.endfunc");
949 index += 3;
950 break;
951 }
952
953 case TOKEN_TYPE::KW_RECORD:
954 index = translate_record(tokens, index, macro_params);
955 break;
956
957 default:
958 // We don't know what it is, assume it's a macro and treat it like an instruction
959 index = translate_instruction(tokens, index, macro_params);
960 break;
961 }
962
963 return index;
964}
unsigned int g_label_number
Definition: asmpp.cpp:297
size_t translate_record(TokenList &tokens, size_t index, const vector< string > &macro_params)
Definition: asmpp.cpp:798
_Self substr(size_type __pos=0, size_type __n=npos) const
Definition: _string.h:1022
size_type size() const
Definition: _string.h:400

Referenced by translate_construct().

◆ translate_instruction()

size_t translate_instruction ( TokenList tokens,
size_t  index,
const vector< string > &  macro_params 
)

Definition at line 607 of file asmpp.cpp.

608{
609 // Translate the instruction itself
610 index = translate_token(tokens, index, macro_params);
611
612 // Handle instruction parameters
613 while (index < tokens.size())
614 {
615 // Optional whitespace
616 if (tokens[index].type() == TOKEN_TYPE::WhiteSpace)
617 {
618 index = translate_token(tokens, index, macro_params);
619 }
620
621 // Check for parameters
622 Token tok = tokens[index];
623 switch (tok.type())
624 {
625 case TOKEN_TYPE::Comment:
626 case TOKEN_TYPE::NewLine:
627 return index;
628
629 case TOKEN_TYPE::WhiteSpace:
630 case TOKEN_TYPE::Operator:
631 index = translate_token(tokens, index, macro_params);
632 break;
633
634 default:
635 index = translate_instruction_param(tokens, index, macro_params);
636 break;
637 }
638 }
639
640 return index;
641}
size_t translate_instruction_param(TokenList &tokens, size_t index, const vector< string > &macro_params)
Definition: asmpp.cpp:557

Referenced by translate_construct(), and translate_identifier_construct().

◆ translate_instruction_param()

size_t translate_instruction_param ( TokenList tokens,
size_t  index,
const vector< string > &  macro_params 
)

Definition at line 557 of file asmpp.cpp.

558{
559 switch (tokens[index].type())
560 {
561 case TOKEN_TYPE::BYTE_PTR:
562 case TOKEN_TYPE::WORD_PTR:
563 case TOKEN_TYPE::DWORD_PTR:
564 case TOKEN_TYPE::QWORD_PTR:
565 case TOKEN_TYPE::XMMWORD_PTR:
566 index = translate_token(tokens, index, macro_params);
567
568 // Optional whitespace
569 if (tokens[index].type() == TOKEN_TYPE::WhiteSpace)
570 {
571 index = translate_token(tokens, index, macro_params);
572 }
573 }
574
575 while (index < tokens.size())
576 {
577 Token tok = tokens[index];
578 switch (tok.type())
579 {
580 case TOKEN_TYPE::MemRefStart:
581 return translate_mem_ref(tokens, index, macro_params);
582
583 case TOKEN_TYPE::NewLine:
584 case TOKEN_TYPE::Comment:
585 return index;
586
587 case TOKEN_TYPE::Operator:
588 if (tok.str() == ",")
589 return index;
590
591 case TOKEN_TYPE::Identifier:
592 index = translate_token(tokens, index, macro_params);
593 if (is_mem_id(tok))
594 {
595 printf("[rip]");
596 }
597 break;
598
599 default:
600 index = translate_expression(tokens, index, macro_params);
601 }
602 }
603
604 return index;
605}
bool is_mem_id(Token &tok)
Definition: asmpp.cpp:309
size_t translate_mem_ref(TokenList &tokens, size_t index, const vector< string > &macro_params)
Definition: asmpp.cpp:524

Referenced by translate_instruction().

◆ translate_item()

size_t translate_item ( TokenList tokens,
size_t  index,
const vector< string > &  macro_params 
)

Definition at line 643 of file asmpp.cpp.

644{
645 switch (tokens[index].type())
646 {
647 case TOKEN_TYPE::DecNumber:
648 case TOKEN_TYPE::HexNumber:
649 case TOKEN_TYPE::String:
650 case TOKEN_TYPE::WhiteSpace:
651 return translate_token(tokens, index, macro_params);
652 }
653
654 throw "Failed to translate item";
655 return -1;
656}

Referenced by translate_data_def(), and translate_list().

◆ translate_list()

size_t translate_list ( TokenList tokens,
size_t  index,
const vector< string > &  macro_params 
)

Definition at line 658 of file asmpp.cpp.

659{
660 while (index < tokens.size())
661 {
662 // The item itself
663 index = translate_item(tokens, index, macro_params);
664
665 // Optional white space
666 if (tokens[index].type() == TOKEN_TYPE::WhiteSpace)
667 {
668 index = translate_token(tokens, index, macro_params);
669 }
670
671 // End of list?
672 if ((tokens[index].type() == TOKEN_TYPE::Comment) ||
673 (tokens[index].type() == TOKEN_TYPE::NewLine))
674 {
675 return index;
676 }
677
678 // We expect a comma here
679 if ((tokens[index].type() != TOKEN_TYPE::Operator) ||
680 (tokens[index].str() != ","))
681 {
682 throw "Unexpected end of list";
683 }
684
685 index = translate_token(tokens, index, macro_params);
686 if (tokens[index].type() == TOKEN_TYPE::WhiteSpace)
687 {
688 index = translate_token(tokens, index, macro_params);
689 }
690 }
691
692 throw "Failed to translate list";
693 return -1;
694}

◆ translate_macro()

size_t translate_macro ( TokenList tokens,
size_t  index 
)

Definition at line 1089 of file asmpp.cpp.

1090{
1091 vector<string> macro_params;
1092
1093 printf(".macro %s", tokens[index].str().c_str());
1094
1095 // Parse marameters
1096 index += 3;
1097 while (index < tokens.size())
1098 {
1099 Token tok = tokens[index];
1100 switch (tok.type())
1101 {
1102 case TOKEN_TYPE::NewLine:
1103 case TOKEN_TYPE::Comment:
1104 index = translate_token(tokens, index, macro_params);
1105 break;
1106
1107 case TOKEN_TYPE::Identifier:
1108 macro_params.push_back(tok.str());
1109 printf("%s", tok.str().c_str());
1110 index++;
1111 continue;
1112
1113 case TOKEN_TYPE::WhiteSpace:
1114 case TOKEN_TYPE::Operator:
1115 index = translate_token(tokens, index, macro_params);
1116 continue;
1117 }
1118
1119 break;
1120 }
1121
1122 // Parse content
1123 while (index < tokens.size())
1124 {
1125 Token tok = tokens[index];
1126 switch (tok.type())
1127 {
1128 case TOKEN_TYPE::KW_ENDM:
1129 printf(".endm");
1130 return index + 1;
1131
1132 default:
1133 index = translate_construct(tokens, index, macro_params);
1134 }
1135 }
1136
1137 throw "Failed to translate macro";
1138 return -1;
1139}
void push_back(const _Tp &__x=_STLP_DEFAULT_CONSTRUCTED(_Tp))
Definition: _vector.h:379

Referenced by translate().

◆ translate_mem_ref()

size_t translate_mem_ref ( TokenList tokens,
size_t  index,
const vector< string > &  macro_params 
)

Definition at line 524 of file asmpp.cpp.

525{
526 unsigned int offset = 0;
527
528 Token tok = tokens[index];
529
530 if ((tok.type() == TOKEN_TYPE::DecNumber) ||
531 (tok.type() == TOKEN_TYPE::HexNumber))
532 {
533 offset = stoi(tok.str(), nullptr, 0);
534 index += 2;
535 }
536
537 index = translate_token(tokens, index, macro_params);
538
539 while (index < tokens.size())
540 {
541 Token tok = tokens[index];
542 index = translate_token(tokens, index, macro_params);
543 if (tok.type() == TOKEN_TYPE::MemRefEnd)
544 {
545 if (offset != 0)
546 {
547 printf(" + %u", offset);
548 }
549 return index;
550 }
551 }
552
553 throw "Failed to translate memory ref";
554 return index;
555}
GLintptr offset
Definition: glext.h:5920

Referenced by translate_instruction_param().

◆ translate_record()

size_t translate_record ( TokenList tokens,
size_t  index,
const vector< string > &  macro_params 
)

Definition at line 798 of file asmpp.cpp.

799{
800 unsigned int bits, bitpos = 0;
801 unsigned long long oldmask = 0, mask = 0;
802
803 Token tok_name = get_expected_token(tokens[index], TOKEN_TYPE::Identifier);
804 index += 4;
805 while (index < tokens.size())
806 {
807 Token tok_member = get_expected_token(tokens[index++], TOKEN_TYPE::Identifier);
808
809 if (tokens[index].type() == TOKEN_TYPE::WhiteSpace)
810 {
811 index++;
812 }
813
814 if (tokens[index++].str() != ":")
815 {
816 throw "Unexpected token";
817 }
818
819 if (tokens[index].type() == TOKEN_TYPE::WhiteSpace)
820 {
821 index++;
822 }
823
824 Token tok_bits = tokens[index++];
825 if ((tok_bits.type() != TOKEN_TYPE::DecNumber) &&
826 (tok_bits.type() != TOKEN_TYPE::HexNumber))
827 {
828 throw "Unexpected token";
829 }
830
831 bits = stoi(tok_bits.str(), nullptr, 0);
832
833 printf("%s = %u\n", tok_member.str().c_str(), bitpos);
834
835 oldmask = (1ULL << bitpos) - 1;
836 bitpos += bits;
837 mask = (1ULL << bitpos) - 1 - oldmask;
838 printf("MASK_%s = 0x%llx\n", tok_member.str().c_str(), mask);
839
840 if (tokens[index].type() == TOKEN_TYPE::WhiteSpace)
841 {
842 index++;
843 }
844
845 if ((tokens[index].type() == TOKEN_TYPE::NewLine) ||
846 (tokens[index].type() == TOKEN_TYPE::Comment))
847 {
848 break;
849 }
850
851 if (tokens[index].str() != ",")
852 {
853 throw "unexpected token";
854 }
855
856 index++;
857 if (tokens[index].type() == TOKEN_TYPE::WhiteSpace)
858 {
859 index++;
860 }
861
862 if ((tokens[index].type() == TOKEN_TYPE::NewLine) ||
863 (tokens[index].type() == TOKEN_TYPE::Comment))
864 {
865 index++;
866 }
867
868 if (tokens[index].type() == TOKEN_TYPE::WhiteSpace)
869 {
870 index++;
871 }
872 }
873
874 return index;
875}
GLenum GLint GLuint mask
Definition: glext.h:6028
GLenum GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * bits
Definition: glext.h:10929
#define bits
Definition: infblock.c:15

Referenced by translate_identifier_construct().

◆ translate_token()

size_t translate_token ( TokenList tokens,
size_t  index,
const vector< string > &  macro_params 
)

Definition at line 382 of file asmpp.cpp.

383{
384 Token tok = tokens[index];
385 switch (tok.type())
386 {
387 case TOKEN_TYPE::Comment:
388 printf("//%s", tok.str().c_str() + 1);
389 break;
390
391 case TOKEN_TYPE::DecNumber:
392 {
393 unsigned long long num = stoull(tok.str(), nullptr, 10);
394 printf("%llu", num);
395 break;
396 }
397
398 case TOKEN_TYPE::HexNumber:
399 {
400 string number = tok.str();
401 printf("0x%s", number.substr(0, number.size() - 1).c_str());
402 break;
403 }
404
405 case TOKEN_TYPE::Identifier:
406 if (is_string_in_list(macro_params, tok.str()))
407 {
408 printf("\\");
409 }
410 printf("%s", tok.str().c_str());
411 break;
412
413 // We migt want to improve these
414 case TOKEN_TYPE::BYTE_PTR:
415 case TOKEN_TYPE::WORD_PTR:
416 case TOKEN_TYPE::DWORD_PTR:
417 case TOKEN_TYPE::QWORD_PTR:
418 case TOKEN_TYPE::XMMWORD_PTR:
419
420 // Check these. valid only in instructions?
421 case TOKEN_TYPE::Reg8:
422 case TOKEN_TYPE::Reg16:
423 case TOKEN_TYPE::Reg32:
424 case TOKEN_TYPE::Reg64:
425 case TOKEN_TYPE::RegXmm:
426 case TOKEN_TYPE::Instruction:
427
428 case TOKEN_TYPE::WhiteSpace:
429 case TOKEN_TYPE::NewLine:
430 case TOKEN_TYPE::Operator:
431 printf("%s", tok.str().c_str());
432 break;
433
434 default:
435 printf("%s", tok.str().c_str());
436 break;
437 }
438
439 return index + 1;
440}
bool is_string_in_list(vector< string > list, string str)
Definition: asmpp.cpp:368
GLuint GLuint num
Definition: glext.h:9618
static unsigned int number
Definition: dsound.c:1479

Referenced by complete_line(), translate_construct(), translate_data_def(), translate_expression(), translate_instruction(), translate_instruction_param(), translate_item(), translate_list(), translate_macro(), and translate_mem_ref().

Variable Documentation

◆ g_identifiers

vector<string> g_identifiers

Definition at line 299 of file asmpp.cpp.

Referenced by add_mem_id(), and is_mem_id().

◆ g_label_number

unsigned int g_label_number = 0

Definition at line 297 of file asmpp.cpp.

Referenced by translate_identifier_construct().

◆ g_TokenList

vector<TOKEN_DEF> g_TokenList

Definition at line 225 of file asmpp.cpp.

Referenced by main().

◆ search_time

time_t search_time

Definition at line 26 of file asmpp.cpp.

Referenced by main(), and Tokenizer::match().