Lhogho  0.0.028
 All Data Structures Files Functions Variables Typedefs Macros Pages
asm.c
Go to the documentation of this file.
1 //
2 // Project: Lhogho
3 // File: asm.c
4 //
5 // Copyright (C) 2007 P.Boytchev
6 //
7 // Revision history:
8 // 2007-06-19 - file created
9 //
10 //
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 2 of the License, or
14 // (at your option) any later version.
15 //
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 //
25 
26 #include <assert.h>
27 #include <stdlib.h>
28 #include <stdarg.h>
29 
30 #include "globals.h"
31 #include "atoms.h"
32 #include "vars.h"
33 #include "lists.h"
34 #include "mems.h"
35 #include "compiler.h"
36 #include "unicode.h"
37 #include "numbers.h"
38 #include "asm.h"
39 #include "options.h"
40 #include "external.h"
41 
42 
43 //===================================================
53 //===================================================
54 void emit_1( context_t* ctx, byte_t data )
55 {
56  if( ctx->generate )
57  {
58  *((byte_t*)(((char*)MEMORY(ctx->generate))+ctx->size)) = data;
59  }
60  ctx->size += sizeof(byte_t);
61 }
62 
63 
64 
65 
66 //===================================================
76 //===================================================
77 void emit_2( context_t* ctx, ushort_t data )
78 {
79  if( ctx->generate )
80  {
81  *((ushort_t*)(((char*)MEMORY(ctx->generate))+ctx->size)) = data;
82  }
83  ctx->size += sizeof(ushort_t);
84 }
85 
86 
87 
88 
89 //===================================================
99 //===================================================
100 void emit_4( context_t* ctx, uint_t data )
101 {
102  if( ctx->generate )
103  {
104  *((uint_t*)(((char*)MEMORY(ctx->generate))+ctx->size)) = data;
105  }
106  ctx->size += sizeof(uint_t);
107 }
108 
109 
110 
111 
112 //===================================================
122 //===================================================
123 void reemit_4( context_t* ctx, uint_t offset, uint_t data )
124 {
125  if( ctx->generate )
126  {
127  *((uint_t*)(((char*)MEMORY(ctx->generate))+offset)) = data;
128  //printf("===fix=%d=%x====\n",data,data);
129  }
130 }
131 
132 
133 
134 #ifdef ADVANCED
135 
136 #define SPACES TEXT(" ")
137 
138 int disasm_pos = 1;
139 
140 #define DISASM_LABEL 1
141 #define DISASM_INSTR 14
142 #define DISASM_PARAM 20
143 #define DISASM_REM 40
144 
145 
146 //===================================================
155 //===================================================
156 void disasm_set_pos( int pos )
157 {
158  // start new line if position cannot be reached
159  if( disasm_pos>=pos )
160  {
161  outter( TEXT("\n"), -1 );
162  disasm_pos = 1;
163  }
164  outter( SPACES, pos-disasm_pos );
165  disasm_pos = pos;
166 }
167 
168 //===================================================
194 //===================================================
195 void disasm( context_t* ctx, int mode, chars_t format, ... )
196 {
197  if( !ASM ) return;
198 
199  // instruction
200  if( mode==1 )
201  {
202  TERM_INSTR;
204  }
205 
206  // info
207  if( mode==2 )
208  {
209  TERM_INFO;
211  if( STRLEN(format)>0 )
212  {
213  outter( TEXT(";; "), -1 );
214  disasm_pos += 3;
215  }
216  }
217 
218  // rem
219  if( mode==3 )
220  {
221  TERM_REMARK;
223  outter( TEXT("; "), -1 );
224  disasm_pos += 2;
225  }
226 
227  // label
228  if( mode==4 )
229  {
230  TERM_INSTR;
232  }
233 
234  va_list param;
235  chars_t ch;
236 
237  #define BUF_SIZE 128
238  char_t buf[BUF_SIZE];
239  chars_t strbuf;
240 
241  va_start (param, format);
242 
243  for( ch=format; *ch; ch++ )
244  {
245  if( *ch != TEXT('%') )
246  {
247  outter( ch, 1 );
248  disasm_pos++;
249  continue;
250  }
251 
252  ch++;
253  switch( *ch )
254  {
255  case TEXT('d'): // n or -n
256  SPRINTF( buf, BUF_SIZE, TEXT("%d"), va_arg(param,int) );
257  outter( buf, -1 );
258  disasm_pos += STRLEN( buf );
259  break;
260  case TEXT('p'): // +n or -n
261  SPRINTF( buf, BUF_SIZE, TEXT("%+d"), va_arg(param,int) );
262  outter( buf, -1 );
263  disasm_pos += STRLEN( buf );
264  break;
265  case TEXT('s'): // string
266  strbuf = va_arg(param,chars_t);
267  outter( strbuf, -1 );
268  disasm_pos += STRLEN( strbuf );
269  break;
270  case TEXT('a'): // atom
271  outter_size = 0;
272  dump_atom( va_arg(param,atom_t), 1 );
274  break;
275  case TEXT('l'): // atom or CAR(atom) if list
276  outter_size = 0;
277  atom_t a = va_arg(param,atom_t);
278  if( IS_LIST(a) && IS_NOT_EMPTY(a) )
279  {
280  dump_atom( CAR(a), 1 );
281  outter( TEXT("..."), 3 );
282  disasm_pos += 3;
283  }
284  else
285  dump_atom( a, 1 );
287  break;
288  }
289  } //for
290 
291  va_end( param );
292 }
293 
294 #endif //ADVANCED
295 
296 #ifdef PROCESSOR_x86
297  #include "i386.c"
298 #endif

[ HOME | INDEX | ATOMS | VARS | REFERENCE ]
Lhogho Developer's Documentation
Wed Jul 10 2013