home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / kaffe-0.5p4-src.tgz / tar.out / contrib / kaffe / kaffevm / jit / labels.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  2KB  |  105 lines

  1. /* labels.c
  2.  * Manage the code labels and links.
  3.  *
  4.  * Copyright (c) 1996 Systems Architecture Research Centre,
  5.  *           City University, London, UK.
  6.  *
  7.  * See the file "license.terms" for information on usage and redistribution
  8.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  9.  *
  10.  * Written by Tim Wilkinson <tim@sarc.city.ac.uk>, June 1996.
  11.  */
  12.  
  13. #include <assert.h>
  14. #include <stdlib.h>
  15. #include "gtypes.h"
  16. #include "labels.h"
  17. #include "codeinfo.h"
  18.  
  19. extern uintp realcodediff;
  20.  
  21. label* firstLabel;
  22. label* lastLabel;
  23. label* currLabel;
  24.  
  25. void
  26. linkLabels(void)
  27. {
  28.     uintp dest;
  29.     uintp from;
  30.     uintp* place;
  31.     label* l;
  32.  
  33.     for (l = firstLabel; l != currLabel; l = l->next) {
  34.  
  35.         if ((l->type & Llong) != Llong) {
  36.             printf("Label not long (%x).\n", l);
  37.             abort();
  38.         }
  39.  
  40.         /* Find destination of jump */
  41.         if (l->type & Lcode) {
  42.             assert(code[l->to].pc != (uintp)-1);
  43.             dest = code[l->to].pc;
  44.         }
  45.         else {
  46.             dest = l->to;
  47.         }
  48.  
  49.         /* If internal destination, adjust it */
  50.         if (l->type & Linternal) {
  51.             dest += realcodediff;
  52.         }
  53.  
  54.         /* Find place to store result */
  55.         place = (uintp*)(l->at + realcodediff);
  56.  
  57.         /* If label is relative, find where it is relative to */
  58.         if (l->type & Lrelative) {
  59.             from = l->from + realcodediff;
  60.         }
  61.         else {
  62.             from = 0;
  63.         }
  64.  
  65.         /* Install result */
  66.         (*place) = dest - from;
  67.     }
  68.  
  69.     /* Reset labels */
  70.     currLabel = firstLabel;
  71. }
  72.  
  73. /*
  74.  * Allocate a new label.
  75.  */
  76. label*
  77. nextLabel(void)
  78. {
  79.     int i;
  80.     label* ret = currLabel;
  81.  
  82.     if (ret == 0) {
  83.         /* Allocate chunk of label elements */
  84.         ret = calloc(ALLOCLABELNR, sizeof(label));
  85.         assert(ret != 0);
  86.  
  87.         /* Attach to current chain */
  88.         if (lastLabel == 0) {
  89.             firstLabel = ret;
  90.         }
  91.         else {
  92.             lastLabel->next = ret;
  93.         }
  94.         lastLabel = &ret[ALLOCLABELNR-1];
  95.  
  96.         /* Link elements into list */
  97.         for (i = 0; i < ALLOCLABELNR-1; i++) {
  98.             ret[i].next = &ret[i+1];
  99.         }
  100.         ret[ALLOCLABELNR-1].next = 0;
  101.     }
  102.     currLabel = ret->next;
  103.     return (ret);
  104. }
  105.