Skip to content

Commit

Permalink
blah
Browse files Browse the repository at this point in the history
  • Loading branch information
ragusaa committed Jan 18, 2024
1 parent 7735281 commit 31ad236
Showing 1 changed file with 10 additions and 230 deletions.
240 changes: 10 additions & 230 deletions clambcc/clambc-compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,22 +120,7 @@ def validate(self) -> bool:
return True


#def run(cmd: str) -> int:
# print ('remove me')
# import pdb ; pdb.set_trace()
# if VERBOSE:
# print(cmd)
# #return os.system(cmd)
#
# ret = os.system(cmd)
## if ret:
# print (cmd)
# print (ret)
# sys.exit(1)
#
# return ret

def runNew(cmd: list) -> int:
def run(cmd: list) -> int:
cmd = ' '.join(cmd)
if VERBOSE:
print(cmd)
Expand Down Expand Up @@ -204,50 +189,6 @@ def compileFile(clangLLVM: ClangLLVM, fileName: str, debugBuild: bool, standardC

outFile = getIrFile(fileName, debugBuild)

"""
includePaths = ""
if options.includes:
for i in options.includes:
includePaths += f"-I{i} "
defines = ""
if options.defines:
for d in options.defines:
defines += f"-D{d} "
print ("TODO: Put clang options in a list")
cmd = f"{clangLLVM.getClang()} \
-S \
-fno-discard-value-names \
-Wno-implicit-function-declaration \
-fno-vectorize \
--language=c \
-emit-llvm \
-Werror=unused-command-line-argument \
-Xclang \
-disable-O0-optnone \
-Xclang -no-opaque-pointers \
{fileName} \
-o \
{outFile} \
-I \
{INCDIR} \
-include \
bytecode.h \
-D__CLAMBC__"
cmd += f" \
{includePaths} \
{defines} \
"
if debugBuild:
cmd += " -g \
"
"""


cmd = []
cmd.append(clangLLVM.getClang())
#cmd.append("-m32") #TODO: Put this back and resolve issues with it.
Expand Down Expand Up @@ -288,7 +229,7 @@ def compileFile(clangLLVM: ClangLLVM, fileName: str, debugBuild: bool, standardC

cmd += options.passthroughOptions

return runNew(cmd)
return run(cmd)


def compileFiles(clangLLVM: ClangLLVM, args: list, debugBuild: bool, standardCompiler: bool, options: Values) -> int:
Expand Down Expand Up @@ -336,21 +277,17 @@ def linkIRFiles(clangLLVM: ClangLLVM, linkedFile: str, irFiles: list) -> int:
Given an output file name and list of IR files, link the IR files.
Returns the exit status code for the call to `llvm-link`.
'''
#inFiles = " ".join(irFiles)
# print ("TODO: Put llvm-link args in a list")
#cmd = f"{clangLLVM.getLLVMLink()} -opaque-pointers=0 -S -o {linkedFile} {inFiles}"

cmd = []
cmd.append(clangLLVM.getLLVMLink())
cmd.append("-S")
cmd.append("-o")
cmd.append(linkedFile)
cmd += irFiles

#TODO: Remove
#TODO: Remove (FUTURE VERSION)
cmd.append("-opaque-pointers=0")

return runNew(cmd)
return run(cmd)


def linkFiles(clangLLVM: ClangLLVM, linkedFile: str, args: list, debugBuild: bool) -> int:
Expand Down Expand Up @@ -548,7 +485,7 @@ def createOptimizedTmpFile(clangLLVM: ClangLLVM, linkedFile: str) -> str:
cmd.append("-internalize-public-api-list=entrypoint")
cmd.append('--passes="internalize,globalopt"')

ret = runNew(cmd)
ret = run(cmd)
if None == ret:
print ("remoev me")
import pdb ; pdb.set_trace()
Expand Down Expand Up @@ -700,112 +637,6 @@ def createInputSourceFile(clangLLVM: ClangLLVM, name: str, args: list, options:

def optimize(clangLLVM: ClangLLVM, inFile: str, outFile: str, sigFile: str, inputSourceFile: str, standardCompiler: bool) -> int:

"""
internalizeAPIList = "_Z10entrypointv,entrypoint,__clambc_kind,__clambc_virusname_prefix,__clambc_virusnames,__clambc_filesize,__clambc_match_counts,__clambc_match_offsets,__clambc_pedata,__Copyright"
# if standardCompiler:
# internalizeAPIList += ",main"
#TODO: Modify ClamBCRemoveUndefs to not require mem2reg to be run before it.
cmd = (f'{clangLLVM.getOpt()} '
f' -S'
f' -verify-each'
f' -load "{SHARED_OBJ_FILE}"'
f' {inFile}'
f' -o {outFile}'
f' -mem2reg'
f' -clambc-remove-undefs' #add pointer bounds checking.
f' -clambc-preserve-abis' #add fake function calls that use all of
#the arguments so that O3 doesn't change
#the argument lists
f' -O3'
f' -clambc-preserve-abis' #remove fake function calls because O3 has already run
f' -clambc-remove-pointer-phis'
f' -dce'
f' -disable-loop-unrolling'
f' -disable-loop-vectorization'
f' -disable-slp-vectorization'
f' -globaldce'
f' -strip-dead-prototypes'
f' -constmerge'
f' -mem2reg'
f' -always-inline'
f' -globalopt'
f' -lowerswitch'
f' -lowerinvoke'
f' -globalopt'
f' -simplifycfg'
f' -indvars'
f' -constprop'
f' -clambc-lowering-notfinal' # perform lowering pass
f' -lowerswitch'
f' -clambc-verifier'
f' -clambc-lowering-notfinal' # perform lowering pass
f' -dce'
f' -simplifycfg'
f' -mem2reg'
f' -clambc-lcompiler' #compile the logical_trigger function to a
#logical signature.
f' -internalize -internalize-public-api-list="{internalizeAPIList}"'
f' -globaldce'
f' -instcombine'
f' -clambc-rebuild'
f' -verify'
f' -simplifycfg'
f' -dce'
f' -lowerswitch'
f' -clambc-verifier'
f' -verify'
f' -strip-debug-declare'
f' -clambc-lowering-final'
f' -clambc-trace'
f' -dce'
f' -clambc-module'
f' -verify'
f' -globalopt'
f' -remove-selects'
f' -clambc-outline-endianness-calls' #outline the endianness calls
#because otherwise the call
#is replaced with a constant
#that is based on where the
#signature was compiled, and
#won't always be accurate.
f' -clambc-change-malloc-arg-size' #make sure we always use the
#64-bit malloc.
f' -globalopt'
f' -clambc-extend-phis-to-64bit' #make all integer phi nodes 64-bit
#because the llvm runtime inserts a
#cast after phi nodes without
#verifying that there is not
#another phi node after it.
f' -clambc-prepare-geps-for-writer' #format gep indexes to not not
#have more than 2, because
#otherwise the writer gets
#unhappy.
f' -globalopt'
f' -clambc-convert-memsets-to-32Bit' #convert all memset intrinsics to
#the 32-bit instead of the 64-bit
#intrinsic
f' -clambc-writer' #write the bytecode
f' -clambc-writer-input-source={inputSourceFile}'
f' -clambc-sigfile={sigFile}'
)
"""

"""
cmd = f"{clangLLVM.getOpt()} %s -o %s %s %s --passes=\"%s\" -clambc-writer-input-source=%s -clambc-sigfile=%s" % (
inFile
, outFile
, " ".join(OPTIMIZE_OPTIONS)
, " ".join(OPTIMIZE_LOADS)
, ",".join(OPTIMIZE_PASSES)
, inputSourceFile
, sigFile
)
"""

cmd = []
cmd.append(clangLLVM.getOpt())
cmd.append(inFile)
Expand All @@ -829,15 +660,10 @@ def optimize(clangLLVM: ClangLLVM, inFile: str, outFile: str, sigFile: str, inpu
cmd.append(f'-clambc-writer-input-source={inputSourceFile}')
cmd.append(f'-clambc-sigfile={sigFile}')

return runNew(cmd)

return run(cmd)

#def genExe(clangLLVM: ClangLLVM, optimizedFile: str, outputFile: str) -> int:
# cmd = f"{clangLLVM.getClang} {optimizedFile} -o {outputFile}"
# return run(cmd)


#This is definitely hacky, but I *think* it's the only change I need to make for
#This is definitely hacky, but it's the only change I need to make for
#this to work
def fixFileSize(optimizedFile: str) -> None:
f = open(optimizedFile)
Expand Down Expand Up @@ -1023,16 +849,11 @@ def main():
parser.add_option(CLANG_BINARY_ARG, dest="clangBinary", help="Path to clang binary")
parser.add_option(OPT_BINARY_ARG, dest="optBinary", help="Path to opt binary")

# parser.add_option("--generate-exe", dest="genexe", action="store_true",
# default=False, help="This is if you want to build a correctly formatted bytecode \
# signature as an executable for debugging (NOT IMPLEMENTED)")
parser.add_option("-I", action="append", dest="includes", default=None)
parser.add_option("-D", action="append", dest="defines", default=None)
parser.add_option("--disable-common-warnings", dest="disableCommonWarnings",
action="store_true", default=True,
help="{%s} (Found in some bytecode signatures)." % (' '.join(COMMON_WARNING_OPTIONS)))
# parser.add_option("--standard-compiler", dest="standardCompiler", action="store_true", default=False,
# help="This is if you want to build a normal c program as an executable to test the compiler.")
(options, args) = parser.parse_args()

if options.version:
Expand All @@ -1044,10 +865,6 @@ def main():
if None == clangLLVM:
sys.exit(1)

#options.genexe = False
#options.standardCompiler = False

#options.passthroughOptions = " ".join(parser.getPassthrough())
options.passthroughOptions = parser.getPassthrough()

if not FOUND_SHARED_OBJ:
Expand All @@ -1062,25 +879,13 @@ def main():
outFile = getOutfile(options, args)
outFile = os.path.basename(outFile)
saveFiles = options.save
#bCompiler = options.standardCompiler
#buildExecutable = bCompiler or options.genexe

createdDir = False

#Add the compiled bytecode file extension, so that all the get<Blahblahblah>Name functions can find it
# if bCompiler:
# idx = outFile.find(COMPILED_BYTECODE_FILE_EXTENSION)
# if -1 == idx:
# outFile += f".{COMPILED_BYTECODE_FILE_EXTENSION}"

if not os.path.isdir(TMPDIR):
os.makedirs(TMPDIR)
createdDir = True

# if options.genexe:
# inFile = os.path.join(os.path.dirname(__file__), 'clambc-compiler-main.c')
# args.append(inFile)
#
res = compileFiles(clangLLVM, args, False, False, options)

if not res:
Expand All @@ -1089,41 +894,13 @@ def main():

if not res:
inputSourceFile = getInputSourceFileName(outFile)
# if bCompiler:
# f = open(inputSourceFile, "w")
# f.close()
# else:
res = createInputSourceFile(clangLLVM, inputSourceFile, args, options)

if not res:
optimizedFile = getOptimizedFileName(outFile)
outFile = getOutfile(options, args)
res = optimize(clangLLVM, linkedFile, optimizedFile, outFile, inputSourceFile, False)

# if not res:
# if options.genexe:
#
# #Add the 'main' and all the stuff that clam provides (TODO: make this configurable by the user)
# mainFile = os.path.join(os.path.dirname(__file__), 'clambc-compiler-main.c')
# res = compileFile(clangLLVM, mainFile, False, False, options)
# if res:
# print("Build FAILED")
# import pdb ; pdb.set_trace()
#
# if not res:
# mainIRFile = getIrFile(mainFile, False)
#
# fixFileSize(optimizedFile)
# fixFileSize(mainIRFile)
#
# res = linkIRFiles(clangLLVM, optimizedFile, [optimizedFile, mainIRFile])
#
# bCompiler = True

# if not res:
# if bCompiler:
# res = genExe(clangLLVM, optimizedFile, outFile)

if ((not saveFiles) and createdDir):
shutil.rmtree(TMPDIR)

Expand All @@ -1136,3 +913,6 @@ def main():
if '__main__' == __name__:

main()



0 comments on commit 31ad236

Please sign in to comment.