7474898eb0c4f7964ce56ce3239b2c20f932fb18
[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 <stdint.h>
24 #include <sys/types.h>
25
26 extern int64_t _index_;
27 extern int64_t _stack_[];
28 extern void _MAIN_();
29
30 void
31 _stacker_dump_stack_()
32 {
33     int64_t i;
34     printf("Stack Dump:\n");
35     for (i = _index_; i > 0; i-- )
36     {
37         printf("#%03lld: %lld\n", (long long int) i, (long long int) _stack_[i] );
38     }
39 }
40
41 int
42 main ( int argc, char** argv )
43 {
44     // Avoid modifying argc
45     int a = argc;
46
47     // Make sure we're starting with the right index
48     _index_ = 0;
49
50     // Copy the arguments to the stack in reverse order
51     // so that they get popped in the order presented
52     while ( a > 0 )
53     {
54         if ( isdigit( (int) argv[--a][0] ) )
55         {
56             _stack_[_index_++] = atoll( argv[a] );
57         }
58         else
59         {
60             _stack_[_index_++] = (int64_t) (intptr_t) argv[a];
61         }
62     }
63
64     // Put the argument count on the stack
65     _stack_[_index_] = argc;
66
67     // Invoke the user's main program
68     _MAIN_();
69
70     // Return last item on the stack
71     if ( _index_ >= 0 )
72         return _stack_[_index_];
73     return -1;
74 }