cfad47cfa3/t3compiler/tads3/test/test_write.cpp

4b825dc642cb6eb9a060e54bf8d69288fbee4904cfad47cfa334b206c65f22086bcc5d63e6f70944
1
/* 
2
 *   Copyright 1999, 2002 Michael J. Roberts
3
 *   
4
 *   Please see the accompanying license file, LICENSE.TXT, for information
5
 *   on using and copying this software.  
6
 */
7
/*
8
 *   Write test T3 image file.  
9
 */
10
11
#include <stdlib.h>
12
#include <stdio.h>
13
14
#include "t3std.h"
15
#include "vmfile.h"
16
#include "vmwrtimg.h"
17
#include "t3test.h"
18
19
/*
20
 *   display an argument and exit 
21
 */
22
static void errexit(const char *msg)
23
{
24
    printf("%s\n", msg);
25
    exit(1);
26
}
27
28
int main(int argc, char **argv)
29
{
30
    CVmFile *fp = 0;
31
    CVmImageWriter *volatile iw = 0;
32
33
    /* initialize for testing */
34
    test_init();
35
36
    /* initialize error stack */
37
    err_init(1024);
38
39
    /* check arguments */
40
    if (argc != 2)
41
        errexit("usage: test_write <output-file>");
42
43
    /* open the file */
44
    fp = new CVmFile();
45
46
    err_try
47
    {
48
        static const char *meta[] =
49
        {
50
            "tads-object"
51
        };
52
        static const char *bif[] =
53
        {
54
            "tads/030000"
55
        };
56
        static const unsigned char code0[] =
57
        {
58
            /* 
59
             *   method header 
60
             */
61
            0x00,                                        /* parameter count */
62
            0x00,                                               /* reserved */
63
            0x02, 0x00,                             /* local variable count */
64
            0x20, 0x00,                              /* maximum stack usage */
65
            0x00, 0x00,                           /* exception table offset */
66
            0x00, 0x00,                         /* debugging records offset */
67
            0x00, 0x00, 0x00, 0x00,                      /* defining object */
68
69
            /*
70
             *   code 
71
             */
72
73
            // local i;   // -> local #0
74
            // local j;   // -> local #1
75
            //
76
            // say('hello, world!\n');
77
            0x05, 0x00, 0x00, 0x00, 0x00, // pushstr  0x00000000
78
            0xB1, 0x00, 0x01,         //    builtin_a 0
79
            
80
            // for (i := 1 ; i < 100 ; ++i)
81
            // {
82
            0xDA, 0x00,               //    onelcl1   0
83
                                      // $1:
84
            0x80, 0x00,               //    getlcl1   0
85
            0x03, 0x64,               //    pushint8  100
86
            0x96, 0x29, 0x00,         //    jgt       $2  ; +41
87
88
            //   j := i*3;
89
            0x80, 0x00,               //    getlcl1   0
90
            0x03, 0x03,               //    pushint8  3
91
            0x24,                     //    mul
92
            0xE0, 0x01,               //    setlcl1   1
93
94
            //   say('i = ' + i + ', j = ' + j + '\n');
95
            0x05, 0x10, 0x00, 0x00, 0x00, // pushstr  0x00000010
96
            0x80, 0x00,               //    getlcl1   0
97
            0x22,                     //    add
98
            0x05, 0x16, 0x00, 0x00, 0x00, // pushstr  0x00000016
99
            0x22,                     //    add
100
            0x80, 0x01,               //    getlcl1   1
101
            0x22,                     //    add
102
            0x05, 0x1E, 0x00, 0x00, 0x00, // pushstr  0x0000001E
103
            0x22,                     //    add
104
            0xB1, 0x00, 0x01,         //    builtin_a 0
105
            
106
            // } // end of for
107
            0xD0, 0x00, 0x00,         //    inclcl    0
108
            0x91, 0xD4, 0xFF,         //    jmp       $1  ; -44
109
                                      // $2:
110
111
            // END OF FUNCTION
112
            0x51                      //    retnil
113
        };
114
        static const unsigned char const0[] =
115
        {
116
            /* 0 = 0x0000 */
117
            /* string "hello, world!\n" */
118
            0x0E, 0x00,  // length = 14
119
            'h', 'e', 'l', 'l', 'o', ',', ' ',
120
            'w', 'o', 'r', 'l', 'd', '!', 0x0A,
121
122
            /* 16 = 0x0010 */
123
            /* string "i = " */
124
            0x04, 0x00,  // length = 4
125
            'i', ' ', '=', ' ',
126
127
            /* 22 = 0x0016 */
128
            /* string ", j = " */
129
            0x06, 0x00,  // length = 6
130
            ',', ' ', 'j', ' ', '=', ' ',
131
132
            /* 30 = 0x001E */
133
            /* string "\n" */
134
            0x01, 0x00,  // length = 1
135
            0x0A
136
        };
137
        static const char tool_data[4] = { 't', 'e', 's', 't' };
138
        
139
        /* open the output file */
140
        fp->open_write(argv[1], OSFTT3IMG);
141
142
        /* create an image writer */
143
        iw = new CVmImageWriter(fp);
144
145
        /* start the file */
146
        iw->prepare(1, tool_data);
147
148
        /* write the entrypoint - enter at code offset zero */
149
        iw->write_entrypt(0, 14, 10, 10, 2, 6, 1);
150
151
        /* write an empty function set dependency table */
152
        iw->write_func_dep(bif, sizeof(bif)/sizeof(bif[0]));
153
154
        /* write our metaclass dependency table */
155
        iw->write_meta_dep(meta, sizeof(meta)/sizeof(meta[0]));
156
157
        /* write our code pool definition and pages */
158
        iw->write_pool_def(1, 1, 4096, TRUE);
159
        iw->write_pool_page(1, 0, (const char *)code0, sizeof(code0),
160
                            TRUE, 0);
161
162
        /* write the constant pool definition and pages */
163
        iw->write_pool_def(2, 1, 4096, TRUE);
164
        iw->write_pool_page(2, 0, (const char *)const0, sizeof(const0),
165
                            TRUE, 0);
166
167
        /* finish the image file data */
168
        iw->finish();
169
    }
170
    err_catch(exc)
171
    {
172
        printf("exception caught: %d\n", exc->get_error_code());
173
    }
174
    err_end;
175
176
    /* delete the image writer */
177
    if (iw != 0)
178
        delete iw;
179
    
180
    /* release our file object */
181
    if (fp != 0)
182
        delete fp;
183
184
    /* delete the error context */
185
    err_terminate();
186
187
    /* done */
188
    return 0;
189
}
190