Skip to content
Open
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
174 changes: 87 additions & 87 deletions routes/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,107 +3,107 @@ util = require('util')

exec = require('child_process').exec
decodeCode=(asm,code)->
# split input
asm_lines = asm.split("\n")
code_lines = code.split("\n")
labels={}
label_data={}
currentlabel=null
files=[]
readmode=false
currentline=0
# split input
asm_lines = asm.split("\n")
code_lines = code.split("\n")
labels={}
label_data={}
currentlabel=null
files=[]
readmode=false
currentline=0

for asline in asm_lines
linedata=/^[ ]+[0-9]+ (.*)/m.exec(asline)
if linedata?
# parse file start markers '.file "filename"'
file=/^[ \t]*\.file[ \t]+([0-9]*)?[ ]?\"([^"]*)"/m.exec(linedata[1])
if file?
fid=if file[1] then file[1] else 0
for asline in asm_lines
linedata=/^[ ]+[0-9]+ (.*)/m.exec(asline)
if linedata?
# parse file start markers '.file "filename"'
file=/^[ \t]*\.file[ \t]+([0-9]*)?[ ]?\"([^"]*)"/m.exec(linedata[1])
if file?
fid=if file[1] then file[1] else 0

fname=file[2]
files[parseInt(fid)]= /(^test|\/test)/.test(fname)
readmode=files[parseInt(fid)]
currentline=0
else
# scan for labels
label=/\.([^ :]*):/m.exec(linedata[1])
if label?
if labels[label[1]]?
labels[label[1]]++
else
if currentlabel? and currentlabel < 1
delete label_data[currentlabel]
fname=file[2]
files[parseInt(fid)]= /(^test|\/test)/.test(fname)
readmode=files[parseInt(fid)]
currentline=0
else
# scan for labels
label=/\.([^ :]*):/m.exec(linedata[1])
if label?
if labels[label[1]]?
labels[label[1]]++
else
if currentlabel? and currentlabel < 1
delete label_data[currentlabel]

labels[label[1]]= 0
label_data[label[1]]= {0:linedata[1]+"\n"}
currentlabel=label[1]
else if currentlabel?
# parse .loc
loc= /^[ \t]*.loc ([0-9]+) ([0-9]+)/.exec(linedata[1])
if loc?
readmode=files[parseInt(loc[1])]
currentline=if readmode then parseInt(loc[2]) else 0
else
if not label_data[currentlabel][currentline]?
label_data[currentlabel][currentline]=""
label_data[currentlabel][currentline]+= linedata[1]+"\n"
if readmode
labels[currentlabel]++
labels[label[1]]= 0
label_data[label[1]]= {0:linedata[1]+"\n"}
currentlabel=label[1]
else if currentlabel?
# parse .loc
loc= /^[ \t]*.loc ([0-9]+) ([0-9]+)/.exec(linedata[1])
if loc?
readmode=files[parseInt(loc[1])]
currentline=if readmode then parseInt(loc[2]) else 0
else
if not label_data[currentlabel][currentline]?
label_data[currentlabel][currentline]=""
label_data[currentlabel][currentline]+= linedata[1]+"\n"
if readmode
labels[currentlabel]++

if currentlabel? and currentlabel < 1
delete label_data[currentlabel]
{code:code_lines,asm:label_data}
if currentlabel? and currentlabel < 1
delete label_data[currentlabel]
{code:code_lines,asm:label_data}


exports.error404 = (req, res)->
res.status(404)
res.render('404', { title: 'C/C++ to Assembly' })

exports.indexpost = (req, res)->
optimize=if req.body.optimize? then "-O2" else ""
lang=if req.body.language=="c" then "c" else "cpp"
optimize=if req.body.optimize? then "-O2" else ""
lang=if req.body.language=="c" then "c" else "cpp"

# generate file name
fileid=Math.floor(Math.random()*1000000001)
compiler=if req.body.arm then "arm-linux-gnueabi-g++-4.6" else "gcc"
asm=if req.body.intel_asm then "-masm=intel" else ""
# generate file name
fileid=Math.floor(Math.random()*1000000001)
compiler=if req.body.arm then "arm-linux-gnueabi-g++-4.6" else "gcc"
asm=if req.body.intel_asm then "-masm=intel" else ""

allowedstandards = [
'c++11'
'c++14'
'c99'
]
allowedstandards = [
'c++11'
'c++14'
'c99'
]

if allowedstandards.indexOf(req.body.standard) > -1
standard = req.body.standard
else
standard = 'c99'
if allowedstandards.indexOf(req.body.standard) > -1
standard = req.body.standard
else
standard = 'c99'

#Write input to file
fs.writeFile "/tmp/test#{fileid}.#{lang}", req.body.ccode, (err)->
if err
res.json({error:"Server Error: unable to write to temp directory"})
else
# execute GCC
compilecmd = "c-preload/compiler-wrapper #{compiler} #{asm} " +
"-std=#{standard} -c #{optimize} -Wa,-ald " +
"-g /tmp/test#{fileid}.#{lang}"
exec compilecmd,
{timeout:10000,maxBuffer: 1024 * 1024*10},
(error, stdout, stderr)->
if error?
# send error message to the client
res.json({error:error.toString()})
fs.unlink("/tmp/test#{fileid}.#{lang}")
fs.unlink("test#{fileid}.o")
else
# parse standard output
blocks=decodeCode(stdout,req.body.ccode)
#Write input to file
fs.writeFile "/tmp/test#{fileid}.#{lang}", req.body.ccode, (err)->
if err
res.json({error:"Server Error: unable to write to temp directory"})
else
# execute GCC
compilecmd = "c-preload/compiler-wrapper #{compiler} #{asm} " +
"-std=#{standard} -c #{optimize} -Wa,-ald " +
"-g /tmp/test#{fileid}.#{lang}"
exec compilecmd,
{timeout:10000,maxBuffer: 1024 * 1024*10},
(error, stdout, stderr)->
if error?
# send error message to the client
res.json({error:error.toString()})
fs.unlink("/tmp/test#{fileid}.#{lang}")
fs.unlink("test#{fileid}.o")
else
# parse standard output
blocks=decodeCode(stdout,req.body.ccode)

# send result as json to the client
res.json(blocks)
# send result as json to the client
res.json(blocks)

# clean up
fs.unlink("/tmp/test#{fileid}.#{lang}")
fs.unlink("test#{fileid}.o")
# clean up
fs.unlink("/tmp/test#{fileid}.#{lang}")
fs.unlink("test#{fileid}.o")