Skip to content

Commit

Permalink
lang/llvmir/parser: Factor out get_implicit_name(), use for implicit …
Browse files Browse the repository at this point in the history
…labels.

LLVM IR has a habbit of using implicit names in various places, from
function params to basic block labels. In such cases, an autogenerated
"numbered name" is used.

Signed-off-by: Paul Sokolovsky <pfalcon@users.sourceforge.net>
  • Loading branch information
pfalcon committed Feb 13, 2021
1 parent aacfd18 commit 2337e60
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions ppci/lang/llvmir/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,16 @@ def __init__(self, context):
self.context = context
self._pfs = None
self.module = None
self.numbered_vals = []
# Counter to form implicit names.
self.numbered_val = 0

# LLVM IR has a habbit of using implicit names in various places, from
# function params to block labels. In this case, a "numbered name" is
# used.
def get_implicit_name(self):
name = "%{}".format(self.numbered_val)
self.numbered_val += 1
return name

def parse_module(self):
""" Parse a module """
Expand Down Expand Up @@ -118,10 +127,8 @@ def parse_function_header(self):
if arg_info.name:
argument.set_name(arg_info.name)
else:
# The parameter had no name, so number it?
name = "%{}".format(len(self.numbered_vals))
name = self.get_implicit_name()
argument.set_name(name)
self.numbered_vals.append((name, argument))
return function

def parse_target_definition(self):
Expand Down Expand Up @@ -238,7 +245,7 @@ def parse_basic_block(self):
if self.peek == "LBL":
label = self.consume("LBL").val
else:
label = None
label = self.get_implicit_name()
bb = self._pfs.define_bb(label)

# Parse instructions until terminator
Expand Down

0 comments on commit 2337e60

Please sign in to comment.