File: module.cpp

package info (click to toggle)
falconpl 0.9.6.9-git20120606-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 46,176 kB
  • sloc: cpp: 181,389; ansic: 109,025; yacc: 2,310; xml: 1,218; sh: 403; objc: 245; makefile: 82; sql: 20
file content (630 lines) | stat: -rw-r--r-- 14,259 bytes parent folder | download | duplicates (2)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
/*
   FALCON - The Falcon Programming Language.
   FILE: module.cpp

   Falcon module manager
   -------------------------------------------------------------------
   Author: Giancarlo Niccolai
   Begin: 2004-08-01

   -------------------------------------------------------------------
   (C) Copyright 2004: the FALCON developers (see list in AUTHORS file)

   See LICENSE file for licensing details.
*/

#include <falcon/module.h>
#include <falcon/memory.h>
#include <falcon/symtab.h>
#include <falcon/types.h>
#include <cstring>
#include <falcon/common.h>
#include <falcon/stream.h>
#include <falcon/string.h>
#include <falcon/traits.h>
#include <falcon/pcodes.h>
#include <falcon/mt.h>
#include <falcon/attribmap.h>

#include <string.h>

namespace Falcon {

const uint32 Module::c_noEntry = 0xFFffFFff;

Module::Module():
   m_refcount(1),
   m_language( "C" ),
   m_modVersion( 0 ),
   m_engineVersion( 0 ),
   m_lineInfo( 0 ),
   m_loader(0),
   m_serviceMap( &traits::t_string(), &traits::t_voidp() ),
   m_attributes(0)
{
   m_strTab = new StringTable;
   m_bOwnStringTable = true;
}


Module::~Module()
{
   // The depend table points to the string table
   // so there is no need to check it.

   // the services in the service table are usually static,
   // ... in case they need to be dynamic, the subclass of
   // ... the module will take care of them.

   delete m_lineInfo;

   delete m_attributes;

   if ( m_bOwnStringTable )
      delete m_strTab;
}

DllLoader &Module::dllLoader()
{
   if ( m_loader == 0 )
      m_loader = new DllLoader;

   return *m_loader;
}

void Module::addDepend( const String &name, const String &module, bool bPrivate, bool bFile )
{
   m_depend.addDependency( name, module, bPrivate, bFile );
}

void Module::addDepend( const String &name, bool bPrivate, bool bFile )
{
   m_depend.addDependency( name, name, bPrivate, bFile );
}


Symbol *Module::addGlobal( const String &name, bool exp )
{
   if ( m_symtab.findByName( name ) != 0 )
      return 0;

   Symbol *sym = new Symbol( this, m_symbols.size(), name, exp );
   sym->setGlobal( );
   m_symbols.push( sym );

   sym->itemId( m_symtab.size() );
   m_symtab.add( sym );
   return sym;
}

void Module::addSymbol( Symbol *sym )
{
   sym->id( m_symbols.size() );
   sym->module( this );
   m_symbols.push( sym );
}

Symbol *Module::addSymbol()
{
   Symbol *sym = new Symbol( this );
   sym->id( m_symbols.size() );
   m_symbols.push( sym );
   return sym;
}

Symbol *Module::addSymbol( const String &name )
{
   Symbol *sym = new Symbol( this, m_symbols.size(), name, false );
   m_symbols.push( sym );
   return sym;
}

Symbol *Module::addGlobalSymbol( Symbol *sym )
{
   if ( sym->id() >= m_symbols.size() || m_symbols.symbolAt( sym->id() ) != sym ) {
      sym->id( m_symbols.size() );
      m_symbols.push( sym );
   }

   sym->itemId( m_symtab.size() );
   sym->module( this );
   m_symtab.add( sym );
   return sym;
}

void Module::adoptStringTable( StringTable *st, bool bOwn )
{
   if ( m_bOwnStringTable )
      delete m_strTab;
   m_strTab = st;
   m_bOwnStringTable = bOwn;
}


Symbol *Module::addConstant( const String &name, int64 value, bool exp )
{
   Symbol *sym = new Symbol( this, name );
   sym->exported( exp );
   sym->setConst( new VarDef( value ) );
   return addGlobalSymbol( sym );
}

Symbol *Module::addConstant( const String &name, numeric value, bool exp )
{
   Symbol *sym = new Symbol( this, name );
   sym->exported( exp );
   sym->setConst( new VarDef( value ) );
   return addGlobalSymbol( sym );
}

Symbol *Module::addConstant( const String &name, const String &value, bool exp )
{
   Symbol *sym = new Symbol( this, name );
   sym->exported( exp );
   sym->setConst( new VarDef( addString( value ) ) );
   return addGlobalSymbol( sym );
}


Symbol *Module::addExtFunc( const String &name, ext_func_t func, void *extra, bool exp )
{
   Symbol *sym = findGlobalSymbol( name );
   if ( sym == 0 )
   {
      sym = addSymbol(name);
      addGlobalSymbol( sym );
   }
   sym->setExtFunc( new ExtFuncDef( func, extra ) );
   sym->exported( exp );
   return sym;
}

Symbol *Module::addFunction( const String &name, byte *code, uint32 size, bool exp )
{
   Symbol *sym = findGlobalSymbol( name );
   if ( sym == 0 )
   {
      sym = addSymbol(name);
      addGlobalSymbol( sym );
   }
   sym->setFunction( new FuncDef( code, size ) );
   sym->exported( exp );
   return sym;
}


Symbol *Module::addClass( const String &name, Symbol *ctor_sym, bool exp )
{
   if ( m_symtab.findByName( name ) != 0 )
      return 0;

   Symbol *sym = new Symbol( this, m_symbols.size(), name, exp );
   sym->setClass( new ClassDef( ctor_sym ) );
   m_symbols.push( sym );

   sym->itemId( m_symtab.size() );
   m_symtab.add( sym );

   return sym;
}

Symbol *Module::addSingleton( const String &name, Symbol *ctor_sym, bool exp )
{
   String clName = "%" + name;

   // symbol or class symbol already present?
   if ( m_symtab.findByName( name ) != 0 || m_symtab.findByName( clName ) != 0 )
      return 0;

   // create the class symbol (never exported)
   Symbol *clSym = addClass( clName, ctor_sym, false );

   // create a singleton instance of the class.
   Symbol *objSym = new Symbol( this, name );
   objSym->setInstance( clSym );
   objSym->exported( exp );
   addGlobalSymbol( objSym );

   return objSym;
}

Symbol *Module::addSingleton( const String &name, ext_func_t ctor, bool exp )
{
   if( ctor != 0 )
   {
      String ctor_name = name + "._init";
      Symbol *sym = addExtFunc( ctor_name, ctor, false );
      if ( sym == 0 )
         return 0;

      return addSingleton( name, sym, exp );
   }
   else
   {
      return addSingleton( name, (Symbol*)0, exp );
   }
}


Symbol *Module::addClass( const String &name, ext_func_t ctor, bool exp )
{
   String ctor_name = name + "._init";
   Symbol *sym = addExtFunc( ctor_name, ctor, false );
   if ( sym == 0 )
      return 0;
   return addClass( name, sym, exp );
}

VarDef& Module::addClassProperty( Symbol *cls, const String &prop )
{
   ClassDef *cd = cls->getClassDef();
   VarDef *vd = new VarDef();
   cd->addProperty( addString( prop ), vd );
   return *vd;
}

VarDef& Module::addClassMethod( Symbol *cls, const String &prop, Symbol *method )
{
   ClassDef *cd = cls->getClassDef();
   VarDef *vd = new VarDef( method );
   cd->addProperty( addString( prop ), vd );
   return *vd;
}

VarDef& Module::addClassMethod( Symbol *cls, const String &prop, ext_func_t method_func )
{
   String name = cls->name() + "." + prop;
   Symbol *method = addExtFunc( name, method_func, false );
   return addClassMethod( cls, prop, method );
}

String *Module::addCString( const char *pos, uint32 size )
{
   String *ret = stringTable().find( pos );
   if ( ret == 0 ) {
      char *mem = (char *)memAlloc(size+1);
      memcpy( mem, pos, size );
      mem[size] = 0;
      ret = new String();
      ret->adopt( mem, size, size + 1 );
      stringTable().add( ret );
   }
   return ret;
}

String *Module::addString( const String &st )
{
   String *ret = stringTable().find( st );
   if ( ret == 0 ) {
      ret = new String( st );
      ret->bufferize();
      ret->exported( st.exported() );
      stringTable().add( ret );
   }
   return ret;
}

String *Module::addString( const String &st, bool exported )
{
   String *ret = stringTable().find( st );
   if ( ret == 0 ) {
      ret = new String( st );
      ret->exported( exported );
      ret->bufferize();
      stringTable().add( ret );
   }
   else {
      ret->exported( exported );
   }

   return ret;
}

uint32 Module::addStringID( const String &st, bool exported )
{
   uint32 ret = stringTable().size();
   String* s = new String( st );
   s->exported( exported );
   s->bufferize();
   stringTable().add( s );

   return ret;
}

bool Module::save( Stream *out, bool skipCode ) const
{
   // save header informations:
   const char *sign = "FM";
   out->write( sign, 2 );

   char ver = pcodeVersion();
   out->write( &ver, 1 );
   ver = pcodeSubVersion();
   out->write( &ver, 1 );

   // serializing module and engine versions
   uint32 iver = endianInt32( m_modVersion );
   out->write( &iver, sizeof( iver ) );
   iver = endianInt32( m_engineVersion );
   out->write( &iver, sizeof( iver ) );

   // serialize the module tables.
   //NOTE: all the module tables are saved 32 bit alinged.

   if ( ! stringTable().save( out ) )
      return false;

   if ( ! m_symbols.save( out ) )
      return false;

   if ( ! m_symtab.save( out ) )
      return false;

   if ( ! m_depend.save( out ) )
      return false;


   if( m_attributes != 0 )
   {
      iver = endianInt32(1);
      out->write( &iver, sizeof( iver ) );

      if ( ! m_attributes->save( this, out ) )
         return false;
   }
   else {
      iver = endianInt32(0);
      out->write( &iver, sizeof( iver ) );
   }

   if ( m_lineInfo != 0 )
   {
      int32 infoInd = endianInt32( 1 );
      out->write( &infoInd, sizeof( infoInd ) );

      if (  ! m_lineInfo->save( out ) )
         return false;
   }
   else {
      int32 infoInd = endianInt32( 0 );
      out->write( &infoInd, sizeof( infoInd ) );
   }

   return out->good();
}

bool Module::load( Stream *is, bool skipHeader )
{
   // verify module version informations.
   if ( !  skipHeader )
   {
      char c;
      is->read( &c, 1 );
      if( c != 'F' )
         return false;
      is->read( &c, 1 );
      if( c != 'M' )
         return false;
      is->read( &c, 1 );
      if ( c != pcodeVersion() )
         return false;
      is->read( &c, 1 );
      if ( c != pcodeSubVersion() )
         return false;
   }

   // Load the module and engine version.
   uint32 ver;
   is->read( &ver, sizeof( ver ) );
   m_modVersion = endianInt32( ver );
   is->read( &ver, sizeof( ver ) );
   m_engineVersion = endianInt32( ver );

   /*TODO: see if there is a localized table around and use that instead.
      If so, skip our internal strtable.
   */
   if ( ! stringTable().load( is ) )
      return false;

   if ( ! m_symbols.load( this, is ) )
      return false;

   if ( ! m_symtab.load( this, is ) )
      return false;

   if ( ! m_depend.load( this, is ) )
      return false;

   is->read( &ver, sizeof( ver ) );
   if( ver != 0 )
   {
      m_attributes = new AttribMap;
      if ( ! m_attributes->load( this, is ) )
         return false;
   }

   // load lineinfo indicator.
   int32 infoInd;
   is->read( &infoInd, sizeof( infoInd ) );
   infoInd = endianInt32( infoInd );
   if( infoInd != 0 )
   {
      m_lineInfo = new LineMap;
      if ( ! m_lineInfo->load( is ) )
         return false;
   }
   else
      m_lineInfo = 0;

   return is->good();
}


uint32 Module::getLineAt( uint32 pc ) const
{
   if ( m_lineInfo == 0 || m_lineInfo->empty() )
      return 0;

   MapIterator iter;
   if( m_lineInfo->find( &pc, iter ) )
   {
      return *(uint32 *) iter.currentValue();
   }
   else {
      iter.prev();
      if( iter.hasCurrent() )
         return *(uint32 *) iter.currentValue();
      return 0;
   }
}

void Module::addLineInfo( uint32 pc, uint32 line )
{
   if ( m_lineInfo == 0 )
      m_lineInfo = new LineMap;

   m_lineInfo->addEntry( pc, line );
}


void Module::setLineInfo( LineMap *infos )
{
   delete m_lineInfo;
   m_lineInfo = infos;
}

Service *Module::getService( const String &name ) const
{
   Service **srv = (Service **) m_serviceMap.find( &name );
   if ( srv != 0 )
   {
      return *srv;
   }
   return 0;
}

bool Module::publishService( Service *sp )
{
   Service **srv = (Service **) m_serviceMap.find( &sp->getServiceName() );
   if ( srv != 0 )
   {
      return false;
   }
   else {
      m_serviceMap.insert( &sp->getServiceName(), sp );
   }
   return true;
}


char Module::pcodeVersion() const
{
   return FALCON_PCODE_VERSION;
}

char Module::pcodeSubVersion() const
{
   return FALCON_PCODE_MINOR;
}

void Module::getModuleVersion( int &major, int &minor, int &revision ) const
{
   major = (m_modVersion ) >> 16;
   minor = (m_modVersion & 0xFF00 ) >> 8;
   revision = m_modVersion & 0xFF;
}

void Module::getEngineVersion( int &major, int &minor, int &revision ) const
{
   major = (m_engineVersion ) >> 16;
   minor = (m_engineVersion & 0xFF00 ) >> 8;
   revision = m_engineVersion & 0xFF;
}

DllLoader *Module::detachLoader()
{
   DllLoader *ret = m_loader;
   m_loader = 0;
   return ret;
}


void Module::incref() const
{
   atomicInc( m_refcount );
}

void Module::decref() const
{
   if( atomicDec( m_refcount ) <= 0 )
   {
      Module *deconst = const_cast<Module *>(this);
      DllLoader *loader = deconst->detachLoader();
      delete deconst;
      delete loader;
   }
}

bool Module::saveTableTemplate( Stream *stream, const String &encoding ) const
{
   stream->writeString( "<?xml version=\"1.0\" encoding=\"" );
   stream->writeString( encoding );
   stream->writeString( "\"?>\n" );
   return stringTable().saveTemplate( stream, name(), language() );
}


void Module::absoluteName( const String &module_name, const String &parent_name, String& result )
{
   if ( module_name.getCharAt(0) == '.' )
   {
      // notation .name
      if ( parent_name.size() == 0 )
         result = module_name.subString( 1 );
      else {
         // remove last part of parent name
         uint32 posDot = parent_name.rfind( "." );
         // are there no dot? -- we're at root elements
         if ( posDot == String::npos )
            result = module_name.subString( 1 );
         else
            result = parent_name.subString( 0, posDot ) + module_name; // "." is included.
      }
   }
   else if ( module_name.find( "self." ) == 0 )
   {
      if ( parent_name.size() == 0 )
         result = module_name.subString( 5 );
      else
         result = parent_name + "." + module_name.subString( 5 );
   }
   else
      result = module_name;
}


void Module::addAttribute( const String &name, VarDef* vd )
{
   if( m_attributes == 0 )
      m_attributes = new AttribMap;

   m_attributes->insertAttrib( name, vd );
}


void Module::rollBackSymbols( uint32 nsize )
{
   uint32 size = symbols().size();

   for (uint32 pos =  nsize; pos < size; pos ++ )
   {
      Symbol *sym = symbols().symbolAt( pos );
      symbolTable().remove( sym->name() );
      delete sym;
   }

   symbols().resize(nsize);
}

}
/* end module.cpp */