1 #!/usr/dcs/software/supported/bin/perl -w
12 my $ROOT = "/tmp/webcompile";
13 #my $ROOT = "/home/vadve/lattner/webcompile";
15 open( STDERR, ">&STDOUT" ) or die "can't redirect stderr to stdout";
17 if ( !-d $ROOT ) { mkdir( $ROOT, 0777 ); }
19 my $LOGFILE = "$ROOT/log.txt";
20 my $FORM_URL = 'index.cgi';
21 my $MAILADDR = 'sabre@nondot.org';
22 my $CONTACT_ADDRESS = 'Questions or comments? Email the <a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev">LLVMdev mailing list</a>.';
23 my $LOGO_IMAGE_URL = 'cathead.png';
24 my $TIMEOUTAMOUNT = 20;
25 $ENV{'LD_LIBRARY_PATH'} = '/home/vadve/shared/localtools/fc1/lib/';
29 '/home/vadve/shared/llvm-gcc4.0-2.1/bin/',
30 '/home/vadve/shared/llvm-2.1/Release/bin');
32 my $defaultsrc = "#include <stdio.h>\n#include <stdlib.h>\n\n" .
33 "int power(int X) {\n if (X == 0) return 1;\n" .
34 " return X*power(X-1);\n}\n\n" .
35 "int main(int argc, char **argv) {\n" .
36 " printf(\"%d\\n\", power(atoi(argv[0])));\n}\n";
40 for ( my $count = 0 ; ; $count++ ) {
42 sprintf( "$ROOT/_%d_%d%s", $$, $count, $extension );
43 if ( !-f $name ) { return $name; }
50 print "<b>", @_, "</b>\n";
52 system("rm -f $ROOT/locked");
57 my $extension = shift @_;
58 my $contents = join "", @_;
59 my $name = getname($extension);
61 open( FILE, ">$name" ) or barf("Can't write to $name: $!");
68 my ( $source, $pid, $result ) = @_;
69 open( LOG, ">>$LOGFILE" );
70 my $time = scalar localtime;
71 my $remotehost = $ENV{'REMOTE_ADDR'};
72 print LOG "[$time] [$remotehost]: $pid\n";
73 print LOG "<<<\n$source\n>>>\nResult is: <<<\n$result\n>>>\n";
78 my ( $header, $file ) = @_;
80 open( FILE, "$file" ) or barf("Can't read $file: $!");
85 my $UnhilightedResult = $result;
87 "<h3>$header</h3>\n<pre>\n" . $c->escapeHTML($result) . "\n</pre>\n";
89 return ( $UnhilightedResult, $HtmlResult );
96 sub syntaxHighlightLLVM {
98 $input =~ s@\b(void|i8|i1|i16|i32|i64|float|double|type|label|opaque)\b@<span class="llvm_type">$1</span>@g;
99 $input =~ s@\b(add|sub|mul|div|rem|and|or|xor|setne|seteq|setlt|setgt|setle|setge|phi|tail|call|cast|to|shl|shr|vaarg|vanext|ret|br|switch|invoke|unwind|malloc|alloca|free|load|store|getelementptr|begin|end|true|false|declare|global|constant|const|internal|uninitialized|external|implementation|linkonce|weak|appending|null|to|except|not|target|endian|pointersize|big|little|volatile)\b@<span class="llvm_keyword">$1</span>@g;
101 # Add links to the FAQ.
102 $input =~ s@(_ZNSt8ios_base4Init[DC]1Ev)@<a href="../docs/FAQ.html#iosinit">$1</a>@g;
103 $input =~ s@\bundef\b@<a href="../docs/FAQ.html#undef">undef</a>@g;
108 my ( $recipient, $body ) = @_;
110 new Mail::Send( Subject => "LLVM Demo Page Run", To => $recipient );
111 my $fh = $msg->open();
122 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
123 <title>Try out LLVM in your browser!</title>
125 \@import url("syntax.css");
126 \@import url("http://llvm.org/llvm.css");
129 <body leftmargin="10" marginwidth="10">
131 <div class="www_sectiontitle">
132 Try out LLVM in your browser!
135 <table border=0><tr><td>
136 <img align=right width=100 height=111 src="$LOGO_IMAGE_URL">
140 if ( -f "$ROOT/locked" ) {
141 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$locktime) =
142 stat("$ROOT/locked");
143 my $currtime = time();
144 if ($locktime + 60 > $currtime) {
145 print "This page is already in use by someone else at this ";
146 print "time, try reloading in a second or two. Meow!</td></tr></table>'\n";
151 system("touch $ROOT/locked");
154 Bitter Melon the cat says, paste a C/C++ program in the text box or upload
155 one from your computer, and you can see LLVM compile it, meow!!
156 </td></tr></table><p>
159 print $c->start_multipart_form( 'POST', $FORM_URL );
161 my $source = $c->param('source');
164 # Start the user out with something valid if no code.
165 $source = $defaultsrc if (!defined($source));
167 print '<table border="0"><tr><td>';
169 print "Type your source code in below: (<a href='DemoInfo.html#hints'>hints and
179 print "Or upload a file: ";
180 print $c->filefield( -name => 'uploaded_file', -default => '' );
185 print '<p></td><td valign=top>';
187 print "<center><h3>General Options</h3></center>";
189 print "Source language: ",
192 -values => [ 'C', 'C++' ],
198 -label => 'Run link-time optimizer',
199 -checked => 'checked'
200 ),' <a href="DemoInfo.html#lto">?</a><br>';
203 -name => 'showstats',
204 -label => 'Show detailed pass statistics'
205 ), ' <a href="DemoInfo.html#stats">?</a><br>';
208 -name => 'cxxdemangle',
209 -label => 'Demangle C++ names'
210 ),' <a href="DemoInfo.html#demangle">?</a><p>';
213 print "<center><h3>Output Options</h3></center>";
216 -name => 'showbcanalysis',
217 -label => 'Show detailed bytecode analysis'
218 ),' <a href="DemoInfo.html#bcanalyzer">?</a><br>';
221 -name => 'showllvm2cpp',
222 -label => 'Show LLVM C++ API code'
223 ), ' <a href="DemoInfo.html#llvm2cpp">?</a>';
225 print "</td></tr></table>";
227 print "<center>", $c->submit(-value=> 'Compile Source Code'),
228 "</center>\n", $c->endform;
230 print "\n<p>If you have questions about the LLVM code generated by the
231 front-end, please check the <a href='/docs/FAQ.html#cfe_code'>FAQ</a> and
232 the demo page <a href='DemoInfo.html#hints'>hints section</a>.
235 $ENV{'PATH'} = ( join ( ':', @PREPENDPATHDIRS ) ) . ":" . $ENV{'PATH'};
237 sub sanitychecktools {
238 my $sanitycheckfail = '';
240 # insert tool-specific sanity checks here
241 $sanitycheckfail .= ' llvm-dis'
242 if `llvm-dis --help 2>&1` !~ /ll disassembler/;
244 $sanitycheckfail .= ' llvm-gcc'
245 if ( `llvm-gcc --version 2>&1` !~ /Free Software Foundation/ );
247 $sanitycheckfail .= ' llvm-ld'
248 if `llvm-ld --help 2>&1` !~ /llvm linker/;
250 $sanitycheckfail .= ' llvm-bcanalyzer'
251 if `llvm-bcanalyzer --help 2>&1` !~ /bcanalyzer/;
254 "<br/>The demo page is currently unavailable. [tools: ($sanitycheckfail ) failed sanity check]"
262 my ( $program, $commandline, $outputFile ) = @_;
266 local $SIG{ALRM} = sub { die "timeout"; };
267 alarm $TIMEOUTAMOUNT;
268 $retcode = system($commandline);
271 if ( $@ and $@ =~ /timeout/ ) {
272 barf("Program $program took too long, compile time limited for the web script, sorry!\n");
274 if ( -s $outputFile ) {
275 print scalar dumpFile( "Output from $program", $outputFile );
277 #print "<p>Finished dumping command output.</p>\n";
278 if ( WIFEXITED($retcode) && WEXITSTATUS($retcode) != 0 ) {
280 "$program exited with an error. Please correct source and resubmit.<p>\n" .
281 "Please note that this form only allows fully formed and correct source" .
282 " files. It will not compile fragments of code.<p>"
285 if ( WIFSIGNALED($retcode) != 0 ) {
286 my $sig = WTERMSIG($retcode);
288 "Ouch, $program caught signal $sig. Sorry, better luck next time!\n"
299 'preprocessed C' => '.i',
300 'preprocessed C++' => '.ii'
306 '.i' => 'preprocessed C',
307 '.ii' => 'preprocessed C++',
313 my $uploaded_file_name = $c->param('uploaded_file');
314 if ($uploaded_file_name) {
317 "You must choose between uploading a file and typing code in. You can't do both at the same time."
320 $uploaded_file_name =~ s/^.*(\.[A-Za-z]+)$/$1/;
321 my $language = $languages{$uploaded_file_name};
322 $c->param( 'language', $language );
324 print "<p>Processing uploaded file. It looks like $language.</p>\n";
325 my $fh = $c->upload('uploaded_file');
327 barf( "Error uploading file: " . $c->cgi_error );
335 if ($c->param('source')) {
337 my $extension = $suffixes{ $c->param('language') };
338 barf "Unknown language; can't compile\n" unless $extension;
340 # Add a newline to the source here to avoid a warning from gcc.
343 # Avoid security hole due to #including bad stuff.
345 s@(\n)?#include.*[<"](.*\.\..*)[">].*\n@$1#error "invalid #include file $2 detected"\n@g;
347 my $inputFile = writeIntoFile( $extension, $source );
350 my $bytecodeFile = getname(".bc");
351 my $outputFile = getname(".llvm-gcc.out");
352 my $timerFile = getname(".llvm-gcc.time");
355 if ( $extension eq ".st" ) {
356 $stats = "-stats -time-passes "
357 if ( $c->param('showstats') );
358 try_run( "llvm Stacker front-end (stkrc)",
359 "stkrc $stats -o $bytecodeFile $inputFile > $outputFile 2>&1",
362 #$stats = "-Wa,--stats,--time-passes,--info-output-file=$timerFile"
363 $stats = "-ftime-report"
364 if ( $c->param('showstats') );
365 try_run( "llvm C/C++ front-end (llvm-gcc)",
366 "llvm-gcc -emit-llvm -W -Wall -O2 $stats -o $bytecodeFile -c $inputFile > $outputFile 2>&1",
370 if ( $c->param('showstats') && -s $timerFile ) {
371 my ( $UnhilightedResult, $HtmlResult ) =
372 dumpFile( "Statistics for front-end compilation", $timerFile );
373 print "$HtmlResult\n";
376 if ( $c->param('linkopt') ) {
378 my $outputFile = getname(".gccld.out");
379 my $timerFile = getname(".gccld.time");
380 $stats = "--stats --time-passes --info-output-file=$timerFile"
381 if ( $c->param('showstats') );
382 my $tmpFile = getname(".bc");
384 "optimizing linker (llvm-ld)",
385 "llvm-ld $stats -o=$tmpFile $bytecodeFile > $outputFile 2>&1",
388 system("mv $tmpFile.bc $bytecodeFile");
389 system("rm $tmpFile");
391 if ( $c->param('showstats') && -s $timerFile ) {
392 my ( $UnhilightedResult, $HtmlResult ) =
393 dumpFile( "Statistics for optimizing linker", $timerFile );
394 print "$HtmlResult\n";
398 print " Bytecode size is ", -s $bytecodeFile, " bytes.\n";
400 my $disassemblyFile = getname(".ll");
402 "llvm-dis -o=$disassemblyFile $bytecodeFile > $outputFile 2>&1",
405 if ( $c->param('cxxdemangle') ) {
406 print " Demangling disassembler output.\n";
407 my $tmpFile = getname(".ll");
408 system("c++filt < $disassemblyFile > $tmpFile 2>&1");
409 system("mv $tmpFile $disassemblyFile");
412 my ( $UnhilightedResult, $HtmlResult );
413 if ( -s $disassemblyFile ) {
414 ( $UnhilightedResult, $HtmlResult ) =
415 dumpFile( "Output from LLVM disassembler", $disassemblyFile );
416 print syntaxHighlightLLVM($HtmlResult);
419 print "<p>Hmm, that's weird, llvm-dis didn't produce any output.</p>\n";
422 if ( $c->param('showbcanalysis') ) {
423 my $analFile = getname(".bca");
424 try_run( "llvm-bcanalyzer", "llvm-bcanalyzer $bytecodeFile > $analFile 2>&1",
427 if ($c->param('showllvm2cpp') ) {
428 my $l2cppFile = getname(".l2cpp");
429 try_run("llvm2cpp","llvm2cpp $bytecodeFile -o $l2cppFile 2>&1",
433 # Get the source presented by the user to CGI, convert newline sequences to simple \n.
434 my $actualsrc = $c->param('source');
435 $actualsrc =~ s/\015\012/\n/go;
436 # Don't log this or mail it if it is the default code.
437 if ($actualsrc ne $defaultsrc) {
438 addlog( $source, $pid, $UnhilightedResult );
440 my ( $ip, $host, $lg, $lines );
441 chomp( $lines = `wc -l < $inputFile` );
442 $lg = $c->param('language');
443 $ip = $c->remote_addr();
444 chomp( $host = `host $ip` ) if $ip;
446 "--- Query: ---\nFrom: ($ip) $host\nInput: $lines lines of $lg\n"
448 . ( $c->param('cxxdemangle') ? 1 : 0 )
450 . ( $c->param('linkopt') ? 1 : 0 ) . "\n\n"
452 . ( $c->param('showstats') ? 1 : 0 ) . "\n\n"
453 . "--- Source: ---\n$source\n"
454 . "--- Result: ---\n$UnhilightedResult\n" );
456 unlink( $inputFile, $bytecodeFile, $outputFile, $disassemblyFile );
459 print $c->hr, "<address>$CONTACT_ADDRESS</address>", $c->end_html;
460 system("rm $ROOT/locked");