Include <sys/types.h> to get the definition of int64_t on Solaris.
[oota-llvm.git] / projects / Stacker / lib / runtime / stacker_rt.c
1 //===-- stacker_rt.c - Runtime Support For Stacker Compiler -----*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Reid Spencer and donated to the LLVM research 
6 // group and is distributed under the University of Illinois Open Source 
7 // License. See LICENSE.TXT for details.
8 // 
9 //===----------------------------------------------------------------------===//
10 //
11 //  This file defines a stack dumping function that can be used for debugging.
12 //  It is called whenever the DUMP built-in word is used in the Stacker source.
13 //  It has no effect on the stack (other than to print it).
14 //
15 //  The real reason this is here is to test LLVM's ability to link with
16 //  separately compiled software.
17 //
18 //===----------------------------------------------------------------------===//
19
20 #include <ctype.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <sys/types.h>
24
25 extern int64_t _index_;
26 extern int64_t _stack_[];
27 extern void _MAIN_();
28
29 void
30 _stacker_dump_stack_()
31 {
32     int64_t i;
33     printf("Stack Dump:\n");
34     for (i = _index_; i > 0; i-- )
35     {
36         printf("#%03lld: %lld\n", i, _stack_[i] );
37     }
38 }
39
40 int
41 main ( int argc, char** argv )
42 {
43     // Avoid modifying argc
44     int a = argc;
45
46     // Make sure we're starting with the right index
47     _index_ = 0;
48
49     // Copy the arguments to the stack in reverse order
50     // so that they get popped in the order presented
51     while ( a > 0 )
52     {
53         if ( isdigit( (int) argv[--a][0] ) )
54         {
55             _stack_[_index_++] = atoll( argv[a] );
56         }
57         else
58         {
59             _stack_[_index_++] = (int) argv[a];
60         }
61     }
62
63     // Put the argument count on the stack
64     _stack_[_index_] = argc;
65
66     // Invoke the user's main program
67     _MAIN_();
68
69     // Return last item on the stack
70     if ( _index_ >= 0 )
71         return _stack_[_index_];
72     return -1;
73 }