1 /******************************************************************************
2 *
3 * Copyright (C) 2008, The Gentee Group. All rights reserved.
4 * This file is part of the Gentee open source project - http://www.gentee.com.
5 *
6 * THIS FILE IS PROVIDED UNDER THE TERMS OF THE GENTEE LICENSE ("AGREEMENT").
7 * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS FILE CONSTITUTES RECIPIENTS
8 * ACCEPTANCE OF THE AGREEMENT.
9 *
10 * ID: gentee 15.01.08 0.0.A.
11 *
12 * Author: Alexey Krivonogov
13 *
14 * Summary:
15 *
16 ******************************************************************************/
17
18 #include "windows.h"
19 #include "windowsx.h"
20 #include "../../src/genteeapi/gentee.h"
21 #include "../../src/bytecode/cmdlist.h"
22 #include "../../src/vm/vmrun.h"
23 #include "../../src/vm/vmload.h"
24 #include "../../src/common/arr.h"
25 #include "stdio.h"
26
27 ubyte curfile[MAX_PATH]; // The current file
28 pubyte out;
29 pubyte filetext;
30 uint vid;
31
32 void printline( uint line )
33 {
34 uint i = 0;
35 uint k = 0;
36 uint count = 1;
37
38 while ( filetext[ i ] && line >= count )
39 {
40 if ( count == line )
41 {
42 out[ k++ ] = filetext[i];
43 }
44 if ( filetext[ i++ ] == 0xa )
45 {
46 count++;
47 }
48 }
49 out[ k ] = 0;
50 printf("%03i:%s", line, out );
51 }
52
53 void funccheck( pubyte name )
54 {
55 if ( mem_cmpign( curfile, name, mem_len( name )))
56 {
57 pvoid handle;
58 uint read;
59
60 mem_copyuntilzero( curfile, name );
61
62 handle = CreateFile( curfile, GENERIC_READ | GENERIC_WRITE,
63 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
64 OPEN_EXISTING, 0, NULL );
65 ReadFile( handle, filetext, GetFileSize( handle, NULL ), &read, NULL );
66 filetext[ read ] = 0;
67 // printf( filetext );
68 CloseHandle( handle );
69 }
70 }
71
72 void printvar( pvartype pvar, uint count, pstackpos curpos, uint off )
73 {
74 ubyte name[64];
75 uint cur;
76 puint top;
77
78 for ( cur = 0; cur < count; cur++ )
79 {
80 if ( curpos )
81 {
82 if ( pvar->flag & VAR_NAME )
83 mem_copyuntilzero( name, pvar->name );
84 else
85 sprintf( name, "tmp%i", vid++ );
86 }
87 else // Global varaiable
88 mem_copyuntilzero( name, ((pvmobj)off)->name );
89
90 printf( "%s[%i]: ", name, pvar->type );
91
92 if ( curpos )
93 {
94 if ( cur + off < curpos->func->parcount )
95 top = curpos->start + pvar->off;
96 else
97 top = ( puint )*( curpos->start + curpos->func->parsize ) +
98 pvar->off;
99 }
100 else // Global varaiable
101 top = ( puint )(( povmglobal )off)->pval;
102
103 switch ( pvar->type )
104 {
105 case TUint:
106 printf("%i ", *top );
107 break;
108 case TStr:
109 if ( pvar->flag & VAR_PARAM )
110 top = ( puint )*top;
111 printf("%s ", (pubyte)*top );
112 break;
113 default:
114 printf("%i ", *top );
115 break;
116 }
117 pvar++;
118 }
119 }
120
121 void printvars( pstackpos curpos )
122 {
123 povmbcode bcode;
124 uint i;
125
126 vid = 0;
127 bcode = ( povmbcode )curpos->func;
128 if ( bcode->vmf.parcount )
129 {
130 printf("------>Params: ");
131 printvar( bcode->vmf.params, bcode->vmf.parcount, curpos, 0 );
132 printf("\n");
133 }
134 if ( bcode->setcount )
135 printf("------>Vars: ");
136
137 for ( i = 0; i < bcode->setcount; i++ )
138 {
139 if ( *( curpos->start + curpos->func->parsize + 1 + i ))
140 printvar( bcode->vars + bcode->sets[i].first, bcode->sets[i].count,
141 curpos, bcode->sets[i].first + bcode->vmf.parcount );
142 }
143 printf("\n");
144 }
145
146 void debugdemo( pstackpos curpos )
147 {
148 funccheck( (pubyte)(( puint )curpos->func->func + 3 ));
149
150 switch ( *curpos->cmd )
151 {
152 case CDbgTrace:
153 printf("Line ");
154 printline( curpos->nline );
155 printvars( curpos );
156 break;
157 case CDbgFunc:
158 printf("===> File: %s\n", (pubyte)(( puint )curpos->func->func + 3 ) );
159 printf("Line ");
160 printline( curpos->nline );
161 break;
162 case CReturn:
163 printf("===> Return\n");
164 printvars( curpos );
165 break;
166 }
167 }
168
169 uint arr_count( parr pa )
170 {
171 return pa->data.use / pa->isize;
172 }
173
174 void globalvars()
175 {
176 pvm vm;
177 uint i;
178 povmglobal pglobal;
179
180 vm = ( pvm )gentee_ptr( GPTR_VM );
181 printf("------>Global vars: ");
182
183 for ( i = 1024; i < arr_count( &vm->objtbl ); i++ )
184 {
185 pglobal = ( povmglobal )*(( puint )vm->objtbl.data.data + i );
186 if ( pglobal->vmo.type == OVM_GLOBAL )
187 {
188 printvar( pglobal->type, 1, 0, ( uint )pglobal );
189 }
190 }
191 }
192 /*
193 typedef struct funcinfo
194 {
195 uint line;
196 uint reserve[32];
197 } funcinfo, * pfuncinfo;
198 */
199 int __cdecl main( int argc, char *argv[] )
200 {
201 compileinfo cmplinfo;
202 uint flag = G_CONSOLE | G_CHARPRN;
203 // uint i;
204
205 printf( "\nDebug Demo\n\
206 Copyright (C) 2008 The Gentee Group. All rights reserved.\n\
207 Internet: http://www.gentee.com Email: info@gentee.com\n\n" );
208 // for ( i = 0; i < 50; i++ )
209 // {
210 gentee_init( flag );
211 gentee_set( GSET_DEBUG, &debugdemo );
212
213 mem_zero( &cmplinfo, sizeof( compileinfo ));
214
215 cmplinfo.flag = CMPL_DEBUG;// | CMPL_THREAD;
216 cmplinfo.defargs = "";
217 cmplinfo.libdirs = "";
218 cmplinfo.include = "..\\..\\exe\\lib\\stdlib.ge\0\0";
219 cmplinfo.defargs = "";
220 cmplinfo.output = "";
221 cmplinfo.input = "..\\debug.g";
222
223 out = ( pubyte )mem_alloc( 0xFFFF );
224 filetext = ( pubyte )mem_alloc( 0xFFFF );
225
226 gentee_compile( &cmplinfo );
227
228 printf("Ret=%i\n", cmplinfo.result );
229 globalvars(); // you can call it in any moment
230 mem_free( out );
231 mem_free( filetext );
232 gentee_deinit();
233 // }
234 getch();
235
236 return 0;
237 }
238