-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rename script and pass in FIRRTL file
- Loading branch information
1 parent
4562a83
commit a87c7f7
Showing
2 changed files
with
53 additions
and
39 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import json | ||
import os | ||
import subprocess | ||
import sys | ||
|
||
# Generates the arguments for m4 based on the JSON data | ||
def generate_m4_arguments(inst): | ||
primitive_name = inst["name"] | ||
args = [] | ||
# hack to replace the module name with the corresponding parameterized version | ||
# FIXME: figure out a way to do substring replacement in m4 | ||
module_name_value = primitive_name | ||
# get the parameters for the primitive | ||
for param in inst["params"]: | ||
key = param["param_name"] | ||
value = param["param_value"] | ||
args.append(f"-D{key}={value}") | ||
module_name_value += "_" + str(value) | ||
|
||
args.append(f"-DMODULE_NAME={module_name_value}") | ||
|
||
# retrieve the appropriate template file for the primitive | ||
firrtl_file_path = os.path.join(sys.path[0], "templates", inst['name'] + ".fir") | ||
args.append(firrtl_file_path) | ||
|
||
return args | ||
|
||
def main(): | ||
if len(sys.argv) != 3: | ||
args_desc = [ | ||
"FIRRTL_FILE", | ||
"PRIMITIVE_USES_JSON", | ||
] | ||
print(f"Usage: {sys.argv[0]} {' '.join(args_desc)}") | ||
return 1 | ||
firrtl_file = open(sys.argv[1]) | ||
primitive_uses_file = open(sys.argv[2]) | ||
# The first line contains the circuit name, which needs to come before the primitives. | ||
print(firrtl_file.readline().rstrip()) | ||
# Display the primitive definitions. | ||
primitive_insts = json.load(primitive_uses_file) | ||
for inst in primitive_insts: | ||
m4_args = ["m4"] | ||
m4_args += generate_m4_arguments(inst) | ||
# execute the subprocess containing m4 | ||
subprocess.run(m4_args) | ||
print() | ||
# Display the rest of the FIRRTL program. | ||
for line in firrtl_file.readlines(): | ||
print(line.rstrip()) | ||
|
||
if __name__ == '__main__': | ||
main() |