aboutsummaryrefslogtreecommitdiff
path: root/projects/Stacker/lib/runtime/stacker_rt.c
blob: 7deb54d6ea388cbfdb8165a8ad1390764bd8459a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//===-- stacker_rt.c - Runtime Support For Stacker Compiler -----*- C++ -*-===//
// 
//                     The LLVM Compiler Infrastructure
//
// This file was developed by Reid Spencer and donated to the LLVM research 
// group and is distributed under the University of Illinois Open Source 
// License. See LICENSE.TXT for details.
// 
//===----------------------------------------------------------------------===//
//
//  This file defines a stack dumping function that can be used for debugging.
//  It is called whenever the DUMP built-in word is used in the Stacker source.
//  It has no effect on the stack (other than to print it).
//
//  The real reason this is here is to test LLVM's ability to link with
//  separately compiled software.
//
//===----------------------------------------------------------------------===//

#include "ctype.h"
#include "stdio.h"
#include "stdlib.h"

extern long _index_;
extern int _stack_[1024];
extern void _MAIN_();

void
_stacker_dump_stack_()
{
    int i;
    printf("Stack Dump:\n");
    for (i = _index_; i > 0; i-- )
    {
	printf("#%03d: %d\n", i, _stack_[i] );
    }
}

int
main ( int argc, char** argv )
{
    // Avoid modifying argc
    int a = argc;

    // Make sure we're starting with the right index
    _index_ = 0;

    // Copy the arguments to the stack in reverse order
    // so that they get popped in the order presented
    while ( a > 0 )
    {
	if ( isdigit( argv[--a][0] ) )
	{
	    _stack_[_index_++] = atoi( argv[a] );
	}
	else
	{
	    _stack_[_index_++] = (int) argv[a];
	}
    }

    // Put the argument count on the stack
    _stack_[_index_] = argc;

    // Invoke the user's main program
    _MAIN_();

    // Return last item on the stack
    if ( _index_ >= 0 )
	return _stack_[_index_];
    return -1;
}