Skip to content

Commit

Permalink
Use click for cdios CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
avanhatt committed Oct 22, 2020
1 parent 80b9254 commit 63a81ca
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 38 deletions.
54 changes: 54 additions & 0 deletions cdios.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env python3

import click
import subprocess
import sys

@click.command()
@click.option('--intermediate',
click.Path(exists=True),
metavar='<dir>',
default='make',
help='command to build submissions')
@click.argument('spec_file',
type=click.Path(exists=True),
metavar='<spec_file>')
def cdios(spec_file, intermediate):
"""Takes a specification file, written in C, and:
- Sanity check that it compiles with GCC
- Translates it to equivalence Racket (best effort)
- Uses Diospyros' equality saturation engine to find vectorization
- Emits C with Tensilica DSP intrinsics
"""
# Attempt to run gcc
cmd = subprocess.run(["gcc", "-S", spec_file, "-o", "/dev/null"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)

# Bail early if GCC fails to compile
# (note: don't assemble since we might not have a main function)
if cmd.returncode:
sys.stdout.write(cmd.stderr.decode("utf-8"))
exit(1)
else:
print("Standard C compilation successful")
print("Writing intermediate files to:".format)

# Nuke output directory contents
subprocess.run(["rm", "-rf", "compile-out/*"], stderr=subprocess.STDOUT)

# Preprocess to handle #defines, etc
subprocess.run(["gcc", "-E", "tmp.c", "-o", "compile-out/preprocess-tmp.c"])

# Remove preprocessing header
subprocess.run(["sed", "/^#/d", "-i", "compile-out/preprocess-tmp.c"])

# Run conversion to Racket
cmd = subprocess.run(['racket', 'src/c-meta.rkt'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if cmd.returncode:
sys.stdout.write(cmd.stderr.decode("utf-8"))
exit(1)

subprocess.run(["make", "compile-egg"], stderr=subprocess.STDOUT)
subprocess.run(["cat", "compile-out/kernel.c"])

if __name__ == '__main__':
cdios()
32 changes: 0 additions & 32 deletions demo/compile.py

This file was deleted.

27 changes: 27 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from setuptools import setup

setup(
name='cdios',
version='1.0.0',
description='A minimal C frontend to Diospyros',
author='Alexa VanHattum',
author_email='[email protected]',
url='https://github.com/cucapra/diospyros',
license='MIT',
platforms='ALL',

install_requires=['click'],

py_modules=['cdios'],

entry_points={
'console_scripts': [
'cdios = cdios:cdios',
],
},

classifiers=[
'Environment :: Console',
'Programming Language :: Python :: 3',
],
)
9 changes: 3 additions & 6 deletions src/c-meta.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@

(require c)

(define program (parse-program (string->path "../demo/preprocess-tmp.c")))
; (define prog (parse-program (string->path "demo/matrix-multiply.c")))

;(define-values (stdout stderr) (gcc (lambda () prog)))
(define program (parse-program (string->path "compile-out/preprocess-tmp.c")))

(assert (eq? (length program) 1))

Expand Down Expand Up @@ -182,7 +179,7 @@
decl:function-preamble
decl:function-body)

; TODO: check return type is void?
; TODO: check return type is void
(quasiquote
(define
(unquote
Expand Down Expand Up @@ -258,7 +255,7 @@
decl:function-body)
(translate (decl:declarator-id decl:function-declarator))]))

(define out-writer (make-spec-out-dir-writer "../demo-out"))
(define out-writer (make-spec-out-dir-writer "compile-out"))

(eval racket-fn ns)

Expand Down

0 comments on commit 63a81ca

Please sign in to comment.