home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / OBJASM.ZIP / ORSEGDEF.C < prev    next >
C/C++ Source or Header  |  1990-11-15  |  6KB  |  199 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #include "o.h"
  5.  
  6. extern int name_compare( NAME_T *, NAME_T * );
  7. void upstr( char *, char * );
  8.  
  9. int seg_compare( SEG_T *, SEG_T * );
  10. void seg_insert( int, NAME_T *, int, int, int );
  11.  
  12. char    *al_text[] = { "?ALIGN",   "BYTE",   "WORD",   "PARA", 
  13.                          "PAGE",  "DWORD", "?ALIGN", "?ALIGN"  };
  14.  
  15. char    *cb_text[] = {          "", "MEMORY", "PUBLIC", "?COMBINE",
  16.                         "?COMBINE",  "STACK", "COMMON", "?COMBINE"  };
  17.  
  18. char    *use_text[] = { "USE16", "USE32" };
  19.  
  20. int seg_compare( rec_1, rec_2 )
  21.     SEG_T  *rec_1;
  22.     SEG_T  *rec_2;
  23. {
  24.     if ( rec_1->index > rec_2->index ) {
  25.         return( LEFT );
  26.     } else {
  27.         if ( rec_1->index < rec_2->index ) {
  28.             return( RIGHT );
  29.         } else {
  30.             return( EQUAL );
  31.         }
  32.     }
  33. }
  34.  
  35. void seg_insert( name_idx, class, length, bit32, code )
  36.     int     name_idx;
  37.     NAME_T  *class;
  38.     int     length;
  39.     int     bit32;
  40.     int     code;
  41. {
  42.     static  segment_count = 0;
  43.     SEG_T   *segment_rec;
  44.  
  45.     segment_count++;
  46.     segment_rec = (SEG_T *)o_malloc( sizeof(SEG_T) );
  47.     segment_rec->index      = segment_count;
  48.     segment_rec->name       = name_idx;
  49.     segment_rec->class      = class;
  50.     segment_rec->length     = length;       /* To infer extra dups          */
  51.     segment_rec->code       = code;
  52.     segment_rec->bit32      = bit32;        /* USE32 segment                */
  53.  
  54.     insert( (char *)segment_rec, segment_tree, seg_compare );
  55. }
  56.  
  57. void upstr( dest, source )
  58.     char     *dest;
  59.     char     *source;
  60. {
  61.     char     ch;
  62.     while ( (ch = *source) != '\0' ) {
  63.         *dest++ = (char)toupper( ch );
  64.         source++;
  65.     }
  66.     *dest = '\0';
  67. }
  68.  
  69. void segdef()
  70. {
  71.     NAME_T          search;
  72.     NAME_T          *segment;
  73.     NAME_T          *class;
  74.     NAME_T          *overlay;
  75.     char            operands[80];
  76.     unsigned char   acbp;
  77.     int             align;
  78.     int             combine;
  79.     int             big;
  80.     int             page;
  81.     int             frame;
  82.     int             ltl;
  83.     unsigned char   offset;
  84.     int             max_len;
  85.     int             grp_off;
  86.     unsigned int    length;
  87.     int             name_idx;
  88.     int             code;
  89.  
  90.     acbp = get_byte();
  91.  
  92.     align   = (acbp & 0xE0) >> 5;
  93.     combine = (acbp & 0x1C) >> 2;
  94.     big     = (acbp & 0x02) >> 1;
  95.     page    = (acbp & 0x01);
  96.     
  97.     if ( align == 0 ) {
  98.         frame  = get_word();
  99.         offset = get_byte();
  100.     }
  101.  
  102.     if ( align == 6 ) {
  103.         ltl = get_byte();
  104.         max_len = get_word();
  105.         grp_off = get_word();
  106.     }
  107.  
  108.     length = get_word();                    /* Length of segment */
  109.  
  110. #ifdef DEBUG
  111. printf("Length of segment = %04X\n",length );
  112. #endif
  113.  
  114.     operands[0] = '\0';                     /* No operands yet */
  115.  
  116.     get_index( &search.index );
  117.     segment = (NAME_T *)find( (char *)&search, name_tree,
  118.                                                     name_compare, NULL );
  119.     if ( segment == NULL ) fmt_error( "Undefined segment name" );
  120.     name_idx = search.index;
  121.  
  122.     get_index( &search.index );
  123.     class = (NAME_T *)find( (char *)&search, name_tree,
  124.                                                     name_compare, NULL );
  125.     if ( class == NULL ) fmt_error( "Undefined class name" );
  126.  
  127.     get_index( &search.index );
  128.     overlay = (NAME_T *)find( (char *)&search, name_tree,
  129.                                                     name_compare, NULL );
  130.     if ( overlay == NULL ) fmt_error( "Undefined overlay name" );
  131.  
  132.     if ( compatibility == 0 ) {
  133.         if ( !processor_type_comment_occurred ) {
  134.             out_line( "", ".386p", "", "Enable USE32/USE16 usage" );
  135.             out_line( "", ".387", "", "Enable floating point also" );
  136.             out_newline();
  137.             processor_type_comment_occurred = TRUE;
  138.             processor_mode = 386;
  139.         }
  140.  
  141.         /* page will now be 0 only for 16-bit segments */
  142.         /* before 32-bit segments                      */
  143.  
  144.         if ( strlen(class->name) ) {
  145.             if ( processor_mode == 386 ) {
  146.                 sprintf( operands, "%s %s %s '%s'", al_text[align],
  147.                         cb_text[combine], use_text[page], class->name );
  148.             } else {
  149.                 sprintf( operands, "%s %s '%s'", al_text[align],
  150.                             cb_text[combine], class->name );
  151.             }
  152.         } else {
  153.             if ( processor_mode == 386 ) {
  154.                 sprintf( operands, "%s %s %s", al_text[align],
  155.                             cb_text[combine], use_text[page] );
  156.             } else {
  157.                 sprintf( operands, "%s %s", al_text[align],
  158.                                               cb_text[combine] );
  159.             }
  160.         }
  161.     } else {
  162.         if ( strlen(class->name) ) {
  163.             sprintf( operands, "%s %s '%s'", al_text[align], 
  164.                                             cb_text[combine], class->name );
  165.         } else {
  166.             sprintf( operands, "%s %s", al_text[align], cb_text[combine] );
  167.         }
  168.     }
  169.  
  170.     if ( compatibility == 2 ) {
  171.         /*
  172.         ** No pre-output segment listing for RASM86 files
  173.         */
  174.     } else {
  175.         out_line( segment->name, "SEGMENT", operands, "" );
  176.         out_line( segment->name, "ENDS", "", "" );
  177.         out_newline();
  178.     }
  179.  
  180.     code = FALSE;
  181.     upstr( operands, segment->name );
  182.     if ( strstr(operands,"CODE") ) {
  183.         code = TRUE;
  184.     }
  185.     if ( strstr(operands, "DRIVER") ) {
  186.         code = TRUE;
  187.     }
  188.  
  189.     upstr( operands, class->name );
  190.     if ( strstr(operands,"CODE") ) {
  191.         code = TRUE;
  192.     }
  193.     if ( strstr(operands,"TEXT") ) {
  194.         code = TRUE;
  195.     }
  196.  
  197.     seg_insert( name_idx, class, length, page, code );
  198. }
  199.