Lhogho  0.0.028
 All Data Structures Files Functions Variables Typedefs Macros Pages
external.h File Reference

Go to the source code of this file.

Data Structures

struct  typeid_rec
 

Macros

#define C_TYPE_UNKNOWN   0
 unknown C-type More...
 
#define C_TYPE_STRUCT   1
 struct C-type More...
 
#define C_TYPE_SIGNED   2
 signed integer C-type More...
 
#define C_TYPE_UNSIGNED   3
 unsigned integer C-type More...
 
#define C_TYPE_FLOAT   4
 float or double C-type More...
 
#define C_TYPE_VOID   5
 void C-type More...
 
#define C_TYPE_POINTER   6
 pointer C-type More...
 
#define C_TYPE_STRING   7
 string C-type More...
 
#define C_TYPE_ATOM   8
 Lhogho atom. More...
 

Typedefs

typedef struct typeid_rec typeid_t
 description structure of a C-type identifiers More...
 

Functions

int get_c_type (int static_link, atom_t parent, atom_t type)
 gets the C-type of a type More...
 
atom_t traverse_pack (int static_link, atom_t parent, atom_t prototype, atom_t protodata, char *ptr, int mode)
 traverses packed data More...
 

Variables

typeid_t c_types []
 

Macro Definition Documentation

#define C_TYPE_UNKNOWN   0

Definition at line 37 of file external.h.

#define C_TYPE_STRUCT   1

Definition at line 38 of file external.h.

#define C_TYPE_SIGNED   2

Definition at line 39 of file external.h.

#define C_TYPE_UNSIGNED   3

Definition at line 40 of file external.h.

#define C_TYPE_FLOAT   4

Definition at line 41 of file external.h.

#define C_TYPE_VOID   5

Definition at line 42 of file external.h.

#define C_TYPE_POINTER   6

Definition at line 43 of file external.h.

#define C_TYPE_STRING   7

Definition at line 44 of file external.h.

#define C_TYPE_ATOM   8

Definition at line 45 of file external.h.

Typedef Documentation

typedef struct typeid_rec typeid_t

Function Documentation

int get_c_type ( int  static_link,
atom_t  parent,
atom_t  type 
)
Parameters
static_linkstatic link from the current frame
parentcurrent parent
typeword containing type name
Returns
index of C-type

This function finds the C-type index of a type. This index can be used with c_types[] array to get additional information about the C-type.

This function does not recurse into struct types.

Definition at line 296 of file external.c.

297 {
298  int c_type;
299 
300 try_again:
301  c_type = type_info( type );
302  if( c_type==C_TYPE_UNKNOWN )
303  {
304  type = type_value( static_link, parent, type );
305  if( IS_UNBOUND(type) ) return C_TYPE_UNKNOWN;
306  goto try_again;
307  }
308  return c_type;
309 }
atom_t traverse_pack ( int  static_link,
atom_t  parent,
atom_t  prototype,
atom_t  protodata,
char *  ptr,
int  mode 
)
Parameters
static_linkstatic link from the current frame
parentcurrent parent
prototypelist describing the c-type in the pack
protodatalist containing the Logo data
ptrpointer to memory with C data
modemode of traversal
Returns
traversal result (depends on the mode)

This function traverses a structure defined by c-type prototype and performs an action determined by mode.

If mode is MEM_STRUCT_SIZE then only the size of the packed data is calculated. Parameters protodata and ptr are not used. The returned value is an integer atom containing the size.

If mode is MEM_STRUCT_PACK then Logo data from protodata is packed into the memory pointed to by ptr using the structure described in prototype. The result is unbound atom.

If mode is MEM_STRUCT_UNPACK then packed data from ptr is unpacked into a list of Logo data using the structure described in prototype . The result of traverse_pack is the list of Logo data.

Definition at line 347 of file external.c.

348 {
349  int ofs = 0;
350  atom_t new_type;
351 
352  atom_t traverse( atom_t prototype, atom_t protodata )
353  {
354  atom_t result = unbound;
355  atom_t result_end;
356 
357  // check prototype list
358  if (IS_ERROR( prototype )) return prototype;
359  if (!IS_LIST( prototype )) return new_error( ERROR_NOT_A_LIST, prototype );
360 
361  // check data list
362  if (IS_ERROR( protodata )) return protodata;
363  if (!IS_LIST( protodata )) return new_error( ERROR_NOT_A_LIST, protodata );
364 
365  if( mode==MEM_STRUCT_UNPACK )
366  {
367  result = empty_list;
368  result_end = empty_list;
369  }
370 
371  int count = 1;
372 
373 #define GET_NEXT_TYPE prototype = CDR( prototype )
374 #define GET_NEXT_DATA protodata = CDR( protodata )
375 
376  // scan all elements of the prototype
377  for (; IS_NOT_EMPTY( prototype ); )
378  {
379  atom_t type = CAR( prototype );
380  atom_t data = CAR( protodata );
381 
382 try_again:
383 
384  // list prototypes are processed recursively
385  if( IS_LIST(type) )
386  {
387  atom_t res = traverse( type, data );
388  if( IS_ERROR(res) ) return res;
389  if( mode==MEM_STRUCT_UNPACK ) append( res, &result, &result_end );
390  goto to_continue;
391  }
392 
393  // other non-word prototypes are not accepted
394  if( IS_INTEGER(type) || IS_FLOAT(type) ) goto its_a_number;
395  if( !IS_ANY_WORD(type) ) return new_error(ERROR_NOT_A_TYPE_NAME,type);
396 
397  int type_id = type_info( type );
398  int type_size = c_types[type_id].size;
399 
400  switch( c_types[type_id].class )
401  {
402  case C_TYPE_POINTER:
403  {
404  int64_t i = 0;
405  if( mode==MEM_STRUCT_PACK )
406  {
407  if( !IS_EMPTY( data ) )
408  {
409  if( IS_MEM(data) )
410  i = (int)MEMORY(data);
411  else
412  GET_INT( data, i );
413  }
414  *(int*)(ptr+ofs) = (int)i;
415  }
416  if( mode==MEM_STRUCT_UNPACK )
417  {
418  i = *(int*)(ptr+ofs);
419  append( new_integer(i), &result, &result_end );
420  }
421  ofs += type_size/8;
422  goto to_continue;
423  }
424  case C_TYPE_ATOM:
425  {
426  atom_t i = 0;
427  if( mode==MEM_STRUCT_PACK )
428  {
429  //*(atom_t*)(ptr+ofs) = data;
430  break;
431  }
432  if( mode==MEM_STRUCT_UNPACK )
433  {
434  i = *(atom_t*)(ptr+ofs);
435  append( USE(i), &result, &result_end );
436  }
437  ofs += type_size/8;
438  goto to_continue;
439  }
440  case C_TYPE_FLOAT:
441  {
442  float64_t i = 0;
443  if( mode==MEM_STRUCT_PACK )
444  {
445  if( !IS_EMPTY( data ) ) GET_FLOAT( data, i );
446  switch( type_size )
447  {
448  case 32: *(float32_t*)(ptr+ofs) = i; break;
449  case 64: *(float64_t*)(ptr+ofs) = i; break;
450  default: goto subtype_test;
451  }
452  }
453  if( mode==MEM_STRUCT_UNPACK )
454  {
455  switch( type_size )
456  {
457  case 32: i = *(float32_t*)(ptr+ofs); break;
458  case 64: i = *(float64_t*)(ptr+ofs); break;
459  default: goto subtype_test;
460  }
461  append( new_float(i), &result, &result_end );
462  }
463  ofs += type_size/8;
464  goto to_continue;
465  }
466 
467  case C_TYPE_SIGNED:
468  {
469  int64_t i = 0;
470  if( mode==MEM_STRUCT_PACK )
471  {
472  if( !IS_EMPTY( data ) ) GET_INT( data, i );
473  switch( type_size )
474  {
475  case 8: *(int8_t*) (ptr+ofs) = i; break;
476  case 16: *(int16_t*)(ptr+ofs) = i; break;
477  case 32: *(int32_t*)(ptr+ofs) = i; break;
478  case 64: *(int64_t*)(ptr+ofs) = i; break;
479  default: goto subtype_test;
480  }
481  }
482  if( mode==MEM_STRUCT_UNPACK )
483  {
484  switch( type_size )
485  {
486  case 8: i = *(int8_t*) (ptr+ofs); break;
487  case 16: i = *(int16_t*)(ptr+ofs); break;
488  case 32: i = *(int32_t*)(ptr+ofs); break;
489  case 64: i = *(int64_t*)(ptr+ofs); break;
490  default: goto subtype_test;
491  }
492  append( new_integer(i), &result, &result_end );
493  }
494  ofs += type_size/8;
495  goto to_continue;
496  }
497  case C_TYPE_UNSIGNED:
498  {
499  int64_t i = 0;
500  if( mode==MEM_STRUCT_PACK )
501  {
502  if( !IS_EMPTY( data ) ) GET_INT( data, i );\
503  switch( type_size )
504  {
505  case 8: *(uint8_t*) (ptr+ofs) = i; break;
506  case 16: *(uint16_t*)(ptr+ofs) = i; break;
507  case 32: *(uint32_t*)(ptr+ofs) = i; break;
508  case 64: *(uint64_t*)(ptr+ofs) = i; break;
509  default: goto subtype_test;
510  }
511  }
512  if( mode==MEM_STRUCT_UNPACK )
513  {
514  switch( type_size )
515  {
516  case 8: i = *(uint8_t*) (ptr+ofs); break;
517  case 16: i = *(uint16_t*)(ptr+ofs); break;
518  case 32: i = *(uint32_t*)(ptr+ofs); break;
519  case 64: i = *(uint64_t*)(ptr+ofs); break;
520  default: goto subtype_test;
521  }
522  append( new_integer(i), &result, &result_end );
523  }
524  ofs += type_size/8;
525  goto to_continue;
526  }
527  }
528 
529  subtype_test:
530  new_type = type_value( static_link, parent, type );
531  if( IS_UNBOUND(new_type) )
532  {
533  its_a_number:
534  if( atom_to_integer( type, &count ) )
535  {
537  continue;
538  }
539  return USE(new_error( ERROR_NOT_A_TYPE_NAME, type ));
540  }
541  type = new_type;
542 
543  goto try_again;
544 
545  to_continue:
546  count--;
547  if( !count )
548  {
549  count = 1;
551  }
553  } //for
554 
555  return result ;
556  }
557 
558  atom_t res = traverse( prototype, protodata );
559  if( IS_ERROR(res) ) return res;
560 
561  // if there is no target pointer, then just return the size
562  if( mode==MEM_STRUCT_SIZE ) return new_integer( ofs );
563 
564  return res;
565 }

Variable Documentation

typeid_t c_types[]

Definition at line 181 of file external.c.


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