Skip to content
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ tools/elf2dol
*.o
*.cp
.vscode
.pragma
29 changes: 29 additions & 0 deletions tools/inlineasm/cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import os
import json
from pathlib import Path

from helpers import hashString

cachePath = "./.pragma/"


def fileCacheName(path):
return hashString(str(path)).hexdigest() + ".json"


def getFileCache(path):
name = fileCacheName(path)
fileCache = {"name": "", "size": 0}
if not os.path.exists(Path(cachePath + name)):
return fileCache
else:
return json.loads(open(Path(cachePath + name)).read())


def saveFileCache(path, fileCache):
name = fileCacheName(path)
open(Path(cachePath + name), "w").write(json.dumps(fileCache, indent=4))


def createCacheFolder():
Path(cachePath).mkdir(parents=True, exist_ok=True)
38 changes: 30 additions & 8 deletions tools/inlineasm/globalasm.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import argparse
from pathlib import Path
from cache import createCacheFolder, getFileCache, saveFileCache
from helpers import *
import os

info = """globalasm.py:

Expand All @@ -11,8 +11,10 @@

parser = argparse.ArgumentParser(description=info)
parser.add_argument("cpFile", help="The .cp file to process")
parser.add_argument(
"-s", "--scope", help="Set function scope equal to scope in assembly", action="store_true")
parser.add_argument("-s",
"--scope",
help="Set function scope equal to scope in assembly",
action="store_true")


def run():
Expand All @@ -22,6 +24,10 @@ def run():
cpText = open(cpPath).read()
matches = getPragmaMatches(cpText)

# Create path to cache if not exists
createCacheFolder()
fileCache = getFileCache(cpPath)

if len(matches) == 0:
return

Expand All @@ -42,21 +48,28 @@ def run():
if key not in asmFileDictionary:
fileText = open(asmPath).read()
funcs = set(getAsmFunctions(fileText))
asmFileDictionary[key] = {
"text": fileText,
"funcs": funcs
}
asmFileDictionary[key] = {"text": fileText, "funcs": funcs}

# check to see if the requested function exists
# before we waste time doing anything
if func + ":" not in asmFileDictionary[key]["funcs"]:
error("function: \"" + func + "\" not in " + key)
error("function: '" + func + "' not in " + key)

# Now let's loop through each pragma and substitute it
for match in matches:

replacePragmaText = match[0]
pragmaArgs = getPragmaArgs(match[1])

funcHash = emptyHash()
funcHash.update(pragmaArgs[0].encode())
funcHash.update(pragmaArgs[1].encode())
hashKey = funcHash.hexdigest()

if hashKey in fileCache:
cpText = cpText.replace(replacePragmaText, fileCache[hashKey])
continue

asmPath = Path(pragmaArgs[0])
key = str(asmPath)
asmFileText = asmFileDictionary[key]["text"]
Expand All @@ -73,7 +86,16 @@ def run():
newSource = writeCode(newSource, funcToImport, codeBytes, isGlobal)
cpText = cpText.replace(replacePragmaText, newSource)

# update our file cache to avoid re-processing
# the same function on every build
fileCache[hashKey] = newSource

open(cpPath, "w").write(cpText)

if fileCache["size"] != len(fileCache):
fileCache["size"] = len(fileCache)
fileCache["name"] = str(cpPath)
saveFileCache(cpPath, fileCache)


run()
11 changes: 11 additions & 0 deletions tools/inlineasm/helpers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
import re
import hashlib


def emptyHash():
return hashlib.sha1()


def hashString(string):
return hashlib.sha1(string.encode())


pragmaRegex = r"(#pragma\sGLOBAL_ASM\((.*?)\))"

Expand All @@ -7,6 +17,7 @@ def getPragmaMatches(fileText):
matches = re.findall(pragmaRegex, fileText, flags=re.DOTALL)
return matches


# Arguments are processed outside of the regex to keep it simple
# and support any changes we may make in the future

Expand Down