Skip to content

[Parser] Start to parse instructions #4789

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions scripts/gen-s-parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ def insert(self, inst, expr):
self.do_insert(inst, inst, expr)


def instruction_parser():
def instruction_parser(new_parser=False):
"""Build a trie out of all the instructions, then emit it as C++ code."""
trie = Node()
inst_length = 0
Expand All @@ -703,12 +703,23 @@ def instruction_parser():

printer = CodePrinter()

printer.print_line("char op[{}] = {{'\\0'}};".format(inst_length + 1))
printer.print_line("strncpy(op, s[0]->c_str(), {});".format(inst_length))
if not new_parser:
printer.print_line("char op[{}] = {{'\\0'}};".format(inst_length + 1))
printer.print_line("strncpy(op, s[0]->c_str(), {});".format(inst_length))

def print_leaf(expr, inst):
printer.print_line("if (strcmp(op, \"{inst}\") == 0) {{ return {expr}; }}"
.format(inst=inst, expr=expr))
if new_parser:
expr = expr.replace("()", "(ctx)")
expr = expr.replace("(s", "(ctx, in")
printer.print_line("if (op == \"{inst}\"sv) {{".format(inst=inst))
with printer.indent():
printer.print_line("auto ret = {expr};".format(expr=expr))
printer.print_line("CHECK_ERR(ret);")
printer.print_line("return *ret;")
printer.print_line("}")
else:
printer.print_line("if (strcmp(op, \"{inst}\") == 0) {{ return {expr}; }}"
.format(inst=inst, expr=expr))
printer.print_line("goto parse_error;")

def emit(node, idx=0):
Expand Down Expand Up @@ -737,7 +748,10 @@ def emit(node, idx=0):
emit(trie)
printer.print_line("parse_error:")
with printer.indent():
printer.print_line("throw ParseException(std::string(op), s.line, s.col);")
if new_parser:
printer.print_line("return in.err(\"unrecognized instruction\");")
else:
printer.print_line("throw ParseException(std::string(op), s.line, s.col);")


def print_header():
Expand All @@ -763,6 +777,8 @@ def main():
sys.exit(1)
print_header()
generate_with_guard(instruction_parser, "INSTRUCTION_PARSER")
print()
generate_with_guard(lambda: instruction_parser(True), "NEW_INSTRUCTION_PARSER")
print_footer()


Expand Down
Loading