[llvm-commits] CVS: llvm-www/demo/cathead.png index.cgi

Brian Gaeke gaeke at cs.uiuc.edu
Wed Nov 12 15:08:01 PST 2003


Changes in directory llvm-www/demo:

cathead.png added (r1.1)
index.cgi added (r1.1)

---
Log message:

Add demo scripts to the CVS


---
Diffs of the changes:  (+264 -0)

Index: llvm-www/demo/cathead.png


Index: llvm-www/demo/index.cgi
diff -c /dev/null llvm-www/demo/index.cgi:1.1
*** /dev/null	Wed Nov 12 15:07:55 2003
--- llvm-www/demo/index.cgi	Wed Nov 12 15:07:45 2003
***************
*** 0 ****
--- 1,264 ----
+ #!/usr/dcs/software/supported/bin/perl -w
+ 
+ use CGI;
+ use POSIX;
+ use Mail::Send;
+ 
+ open( STDERR, ">&STDOUT" ) or die "can't redirect stderr to stdout";
+ 
+ if (! -d "/tmp/webcompile") { mkdir ("/tmp/webcompile", 0777); }
+ 
+ $LOGFILE           = '/tmp/webcompile/log.txt';
+ $FORM_TITLE        = 'Try out LLVM in your Browser!';
+ $FORM_URL          = 'http://llvm.cs.uiuc.edu/demo/index.cgi';
+ $CONTACT_ADDRESS   = 'gaeke -at- uiuc.edu';
+ $LOGO_IMAGE_URL    = 'cathead.png';
+ $EXPLAIN_PARAGRAPH = <<END;
+  Bitter Melon the cat says, paste a C/C++ program in the text box or upload one
+  from your computer, and you can see the equivalent LLVM code, meow!!
+  But no sneaky pasting in syntax errors, now...
+  Oh, and if you're considering uploading a file from a larger C program,
+  consider preprocessing it (e.g., with the <tt>-save-temps</tt> or
+  <tt>-E</tt> options to <tt>gcc</tt>) first.
+ END
+ @PREPENDPATHDIRS =
+   ( '/home/vadve/gaeke/llvm/tools/Debug', '/home/vadve/gaeke/bin' );
+ $ENV{'LLVM_LIB_SEARCH_PATH'} =
+   '/home/vadve/lattner/local/x86/llvm-gcc/bytecode-libs';
+ $ENV{'LD_LIBRARY_PATH'} = '/home/vadve/shared/localtools/x86/lib';
+ 
+ sub getname {
+     my ($extension) = @_;
+     for ( my $count = 0 ; ; $count++ ) {
+         my $name = sprintf( "/tmp/webcompile/_%d_%d%s", $$, $count, $extension );
+         if ( !-f $name ) { return $name; }
+     }
+ }
+ 
+ sub barf {
+     print "<b>", @_, "</b>\n";
+     print $c->end_html;
+     exit 1;
+ }
+ 
+ sub writeIntoFile {
+     my $extension = shift @_;
+     my $contents  = join "", @_;
+     my $name      = getname($extension);
+     local (*FILE);
+     open( FILE, ">$name" ) or barf("Can't write to $name: $!");
+     print FILE $contents;
+     close FILE;
+     return $name;
+ }
+ 
+ sub addlog {
+     my ( $source, $pid, $result ) = @_;
+     open( LOG, ">>$LOGFILE" );
+     my $time       = scalar localtime;
+     my $remotehost = $ENV{'REMOTE_ADDR'};
+     print LOG "[$time] [$remotehost]: $pid\n";
+     print LOG "<<<\n$source\n>>>\nResult is: <<<\n$result\n>>>\n";
+     close LOG;
+ }
+ 
+ sub dumpFile {
+     my ( $header, $file ) = @_;
+     my $result;
+     open( FILE, "$file" ) or barf("Can't read $file: $!");
+     while (<FILE>) {
+         $result .= $_;
+     }
+     close FILE;
+     my $UnhilightedResult = $result;
+     my $HtmlResult = "<h3>$header</h3>\n<pre>\n"
+                    . $c->escapeHTML($result)
+                    . "\n</pre>\n";
+ 	if (wantarray) {
+ 		return ($UnhilightedResult, $HtmlResult);
+ 	} else {
+ 		return $HtmlResult;
+ 	}
+ }
+ 
+ sub syntaxHighlightLLVM {
+     my ($input) = @_;
+     $input =~
+ s@\b(void|bool|sbyte|ubyte|short|ushort|int|uint|long|ulong|float|double|type|label|opaque)\b@<i>$1</i>@g;
+     $input =~
+ s@\b(add|sub|mul|div|rem|and|or|xor|setne|seteq|setlt|setgt|setle|setge|phi|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@<b>$1</b>@g;
+     return $input;
+ }
+ 
+ sub mailto {
+ 	my ($recipient, $body) = @_;
+ 	my $msg = new Mail::Send (Subject=>"LLVM Demo Page Run", To=>$recipient);
+     local ($fh);
+ 	$fh = $msg->open();
+ 	print $fh $body;
+ 	$fh->close();
+ }
+ 
+ $c = new CGI;
+ print $c->header,
+   $c->start_html(
+     -title   => $FORM_TITLE,
+     -BGCOLOR => '#99bbff'
+   ),
+   $c->h3($FORM_TITLE);
+ if ($LOGO_IMAGE_URL) { print "<img src=\"$LOGO_IMAGE_URL\" />"; }
+ 
+ print "<p>$EXPLAIN_PARAGRAPH</p>",
+   $c->start_multipart_form( 'POST', $FORM_URL );
+ 
+ print "Upload a file: ";
+ print $c->filefield( -name => 'uploaded_file', -default => '' );
+ print "<p>Or type your ";
+ print $c->radio_group(
+     -name    => 'language',
+     -values  => [ 'C', 'C++' ],
+     -default => 'C'
+ );
+ print " source text below:</p>";
+ 
+ $source = $c->param('source');
+ print $c->textarea(
+     -name    => "source",
+     -rows    => 12,
+     -columns => 80,
+     -default => $source
+ );
+ 
+ print "<p />";
+ print $c->checkbox(
+     -name  => 'cxxdemangle',
+     -label =>
+ 'Demangle C++ names in output using c++filt (warning: may produce asyntactic output)'
+ );
+ 
+ print "<p>", $c->submit, "\n", $c->endform;
+ 
+ $ENV{'PATH'} = ( join ( ':', @PREPENDPATHDIRS ) ) . ":" . $ENV{'PATH'};
+ 
+ sub sanitychecktools {
+     my $sanitycheckfail = '';
+ 
+     # insert tool-specific sanity checks here
+     $sanitycheckfail .= ' gccas' if `gccas --help 2>&1` !~ /assembler for GCC/;
+     $sanitycheckfail .= ' llvm-dis'
+       if `llvm-dis --help 2>&1` !~ /ll disassembler/;
+     $sanitycheckfail .= ' llvm-gcc'
+       if `llvm-gcc --version 2>&1` !~ /Free Software Foundation/;
+     barf(
+ "The demo page is currently unavailable. [tools: ($sanitycheckfail ) failed sanity check]"
+       )
+       if $sanitycheckfail;
+ }
+ sanitychecktools();
+ 
+ sub try_run {
+     my ( $program, $commandline, $outputFile ) = @_;
+     my $retcode = system($commandline);
+     if ( -s $outputFile ) {
+         print dumpFile( "Output from $program", $outputFile );
+     }
+     if ( WIFEXITED($retcode) && WEXITSTATUS($retcode) != 0 ) {
+         barf(
+ "$program exited with an error. Please correct source and resubmit.\n"
+         );
+     }
+     if ( WIFSIGNALED($retcode) != 0 ) {
+         my $sig = WTERMSIG($retcode);
+         barf(
+             "Ouch, $program caught signal $sig. Sorry, better luck next time!\n"
+         );
+     }
+ }
+ 
+ %suffixes = (
+     'Java'              => '.java',
+     'JO99'              => '.jo9',
+     'C'                 => '.c',
+     'C++'               => '.cc',
+     'preprocessed C'    => '.i',
+     'preprocessed C++'  => '.ii'
+ );
+ %languages = (
+     '.jo9'  => 'JO99',
+     '.java' => 'Java',
+     '.c'    => 'C',
+     '.i'    => 'preprocessed C',
+     '.ii'   => 'preprocessed C++',
+     '.cc'   => 'C++',
+     '.cpp'  => 'C++'
+ );
+ 
+ $uploaded_file_name = $c->param('uploaded_file');
+ if ($uploaded_file_name) {
+     if ($source) {
+         barf(
+ "You must choose between uploading a file and typing code in. You can't do both at the same time."
+         );
+     }
+     $uploaded_file_name =~ s/^.*(\.[A-Za-z]+)$/$1/;
+     my $language = $languages{$uploaded_file_name};
+     $c->param( 'language', $language );
+ 
+     print "<p>Processing uploaded file. It looks like $language.</p>\n";
+     my $fh = $c->upload('uploaded_file');
+     if ( !$fh ) {
+         barf( "Error uploading file: " . $c->cgi_error );
+     }
+     while (<$fh>) {
+         $source .= $_;
+     }
+     close $fh;
+ }
+ 
+ if ($source) {
+     print $c->hr;
+     my $extension = $suffixes{ $c->param('language') };
+     barf "Unknown language; can't compile\n" unless $extension;
+ 
+     # Add a newline to the source here to avoid a warning from gcc.
+     $source .= "\n";
+ 
+     $source =~
+ s@(\n)?#include.*[<"](.*\.\..*)[">].*\n@$1#error "invalid #include file $2 detected"\n at g;
+ 
+     my $inputFile = writeIntoFile( $extension, $source );
+     my $pid = $$;
+ 
+     my $bytecodeFile = getname(".o");
+     my $outputFile   = getname(".llvm-gcc.out");
+     try_run( "llvm-gcc",
+         "llvm-gcc -o $bytecodeFile -c $inputFile > $outputFile 2>&1",
+         $outputFile );
+     my $disassemblyFile = getname(".ll");
+     try_run( "llvm-dis",
+         "llvm-dis -o=$disassemblyFile $bytecodeFile > $outputFile 2>&1",
+         $outputFile );
+ 
+     if ( $c->param('cxxdemangle') ) {
+         print "<p>Demangling disassembler output.</p>\n";
+         my $tmpFile = getname(".ll");
+         system("c++filt < $disassemblyFile > $tmpFile 2>&1");
+         system("mv $tmpFile $disassemblyFile");
+     }
+ 
+     my ($UnhilightedResult, $HtmlResult);
+     if ( -s $disassemblyFile ) {
+         ($UnhilightedResult, $HtmlResult)
+           = dumpFile( "Output from LLVM disassembler", $disassemblyFile );
+         print syntaxHighlightLLVM($HtmlResult);
+     } else {
+         print "<p>Hmm, that's weird, llvm-dis didn't produce any output.</p>\n";
+         unlink( $inputFile, $bytecodeFile, $outputFile, $disassemblyFile );
+     }
+     addlog( $source, $pid, $UnhilightedResult );
+     mailto( 'lattner at cs.uiuc.edu',
+             "--- Source: ---\n$source\n--- Result: ---\n$UnhilightedResult\n" );
+ }
+ 
+ print $c->hr, "<address>$CONTACT_ADDRESS</address>", $c->end_html;
+ exit 0;





More information about the llvm-commits mailing list