home *** CD-ROM | disk | FTP | other *** search
/ Java 1996 August / Java - Summer 1996.iso / kaffe-0.2 / kaffeh / constants.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-17  |  2.9 KB  |  159 lines

  1. /*
  2.  * constants.c
  3.  * Constant management.
  4.  *
  5.  * Copyright (c) 1996 Systems Architecture Research Centre,
  6.  *           City University, London, UK.
  7.  *
  8.  * See the file "license.terms" for information on usage and redistribution
  9.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  10.  *
  11.  * Written by Tim Wilkinson <tim@sarc.city.ac.uk>, February 1996.
  12.  */
  13.  
  14. #define    RDBG(s)
  15.  
  16. #include <stdio.h>
  17. #include <assert.h>
  18. #include "gtypes.h"
  19. #include "constants.h"
  20.  
  21. static strconst* strhash[STRHASHSZ];
  22.  
  23. /*
  24.  * Read in constant pool from opened file.
  25.  */
  26. constants*
  27. readConstantPool(FILE* fp)
  28. {
  29.     constants* info;
  30.     u4* pool;
  31.     u1* tags;
  32.     int i;
  33.     u1 type;
  34.     u2 len;
  35.     strconst* c;
  36.     char* uc;
  37.     u1 d1;
  38.     u2 d2, d2b;
  39.     u4 d4;
  40.  
  41.     info = (constants*)malloc(sizeof(constants));
  42.     if (info == 0) {
  43.         return (0);
  44.     }
  45.  
  46.     readu2(&info->size, fp);
  47. RDBG(    printf("constant_pool_count=%d\n", info->size);    )
  48.  
  49.     /* Allocate space for tags and data */
  50.     pool = (u4*)malloc((sizeof(u4) + sizeof(u1)) * info->size);
  51.     if (pool == 0) {
  52.         return (0);
  53.     }
  54.     tags = (u1*)&pool[info->size];
  55.     info->data = pool;
  56.     info->tags = tags;
  57.  
  58.     pool[0] = 0;
  59.     tags[0] = CONSTANT_Unknown;
  60.     for (i = 1; i < info->size; i++) {
  61.  
  62.         readu1(&type, fp);
  63. RDBG(        printf("Constant type %d\n", type);            )
  64.         tags[i] = type;
  65.  
  66.         if (type == CONSTANT_Utf8) {
  67.             readu2(&len, fp);
  68.             c = (strconst*)malloc(sizeof(strconst) + len + 1);
  69.             if (c == 0) {
  70.                 return (0);
  71.             }
  72.             fread(c->data, len, sizeof(u1), fp);
  73.             c->data[len] = 0;
  74. RDBG(            printf("Utf8=%s\n", c->data);            )
  75.  
  76.             pool[i] = (u4)addStringConstant(c);
  77.         }
  78.         else if (type == CONSTANT_Unicode) {
  79.             abort();
  80.             readu2(&len, fp);
  81.             uc = (u1*)malloc(len * 2 + 1);
  82.             fread(uc, len, sizeof(u2), fp);
  83.             uc[len * 2] = 0;
  84.             abort();
  85.         }
  86.         else switch (type) {
  87.         case CONSTANT_Class:
  88.             readu2(&d2, fp);
  89.             pool[i] = d2;
  90.             break;
  91.  
  92.         case CONSTANT_String:
  93.             readu2(&d2, fp);
  94.             pool[i] = d2;
  95.             tags[i] = CONSTANT_Chararray;
  96.             break;
  97.  
  98.         case CONSTANT_Fieldref:
  99.         case CONSTANT_Methodref:
  100.         case CONSTANT_InterfaceMethodref:
  101.         case CONSTANT_NameAndType:
  102.             readu2(&d2, fp);
  103.             readu2(&d2b, fp);
  104.             pool[i] = (d2b << 16) | d2;
  105.             break;
  106.  
  107.         case CONSTANT_Integer:
  108.         case CONSTANT_Float:
  109.             readu4(&d4, fp);
  110.             pool[i] = d4;
  111.             break;
  112.  
  113.         case CONSTANT_Long:
  114.         case CONSTANT_Double:
  115.             readu4(&d4, fp);
  116.             pool[i] = d4;
  117.             i++;
  118.             readu4(&d4, fp);
  119.             tags[i] = CONSTANT_Unknown;
  120.             pool[i] = d4;
  121.             break;
  122.  
  123.         default:
  124.             fprintf(stderr, "Bad constant %d\n", type);
  125.             return (0);
  126.         }
  127.     }
  128.  
  129.     return (info);
  130. }
  131.  
  132. /*
  133.  * Add a string to the string pool.
  134.  * If already there, free the new one and return the old one.
  135.  */
  136. char*
  137. addStringConstant(strconst* s)
  138. {
  139.     strconst* ptr;
  140.     uint32 hash;
  141.     int i;
  142.  
  143.     for (hash = 0,i = 0; s->data[i] != 0; i++) {
  144.         hash = hash * 33 + s->data[i];
  145.     }
  146.     hash %= STRHASHSZ;
  147.  
  148.     for (ptr = strhash[hash]; ptr != 0; ptr = ptr->next) {
  149.         if (strcmp(ptr->data, s->data) == 0) {
  150.             free(s);
  151.             return (ptr->data);
  152.         }
  153.     }
  154.     s->next = strhash[hash];
  155.     s->string = 0;
  156.     strhash[hash] = s;
  157.     return (s->data);
  158. }
  159.