summaryrefslogtreecommitdiffstats
path: root/byterun/extern.c
blob: 4be22d62ada8b00b980fa7ee6e5ead87179a2bb1 (plain)
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
/* Structured output */

#include "fail.h"
#include "gc.h"
#include "intext.h"
#include "io.h"
#include "memory.h"
#include "misc.h"
#include "mlvalues.h"
#include "reverse.h"
#include "str.h"

/* To keep track of sharing in externed objects */

typedef unsigned long byteoffset_t;

struct extern_obj {
  value obj;
  byteoffset_t ofs;
};

static struct extern_obj * extern_table;
static asize_t extern_table_size;

#ifdef SIXTYFOUR
#define Hash(v) (((asize_t) ((v) >> 3)) % extern_table_size)
#else
#define Hash(v) (((asize_t) ((v) >> 2)) % extern_table_size)
#endif

static void alloc_extern_table()
{
  asize_t i;

  extern_table = (struct extern_obj *)
    stat_alloc(extern_table_size * sizeof(struct extern_obj));
  for (i = 0; i < extern_table_size; i++)
    extern_table[i].obj = 0;
}

static void resize_extern_table()
{
  asize_t oldsize;
  struct extern_obj * oldtable;
  asize_t i, h;

  oldsize = extern_table_size;
  oldtable = extern_table;
  extern_table_size = 2 * extern_table_size;
  alloc_extern_table();
  for (i = 0; i < oldsize; i++) {
    h = Hash(oldtable[i].obj);
    while (extern_table[h].obj != 0) {
      h++;
      if (h >= extern_table_size) h = 0;
    }
    extern_table[h].obj = oldtable[i].obj;
    extern_table[h].ofs = oldtable[i].ofs;
  }
  stat_free((char *) oldtable);
}

/* Write integers on a channel */

static void output8(chan, code, val)
     struct channel * chan;
     int code;
     long val;
{
  putch(chan, code); putch(chan, val);
}

static void output16(chan, code, val)
     struct channel * chan;
     int code;
     long val;
{
  putch(chan, code); putch(chan, val >> 8); putch(chan, val);
}

static void output32(chan, code, val)
     struct channel * chan;
     int code;
     long val;
{
  putch(chan, code);
  putch(chan, val >> 24); putch(chan, val >> 16);
  putch(chan, val >> 8); putch(chan, val);
}

#ifdef SIXTYFOUR
static void output64(chan, code, val)
     struct channel * chan;
     int code;
     long val;
{
  int i;
  putch(chan, code);
  for (i = 64 - 8; i >= 0; i -= 8) putch(chan, val >> i);
}
#endif

static byteoffset_t obj_counter;    /* Number of objects emitted so far */
static unsigned long size_32;  /* Size in words of 32-bit block for struct. */
static unsigned long size_64;  /* Size in words of 64-bit block for struct. */

static void emit_compact(chan, v)
     struct channel * chan;
     value v;
{
 tailcall:
  if (Is_long(v)) {
    long n = Long_val(v);
    if (n >= 0 && n < 0x40) {
      putch(chan, PREFIX_SMALL_INT + n);
    } else if (n >= -(1 << 7) && n < (1 << 7)) {
      output8(chan, CODE_INT8, n);
    } else if (n >= -(1 << 15) && n < (1 << 15)) {
      output16(chan, CODE_INT16, n);
#ifdef SIXTYFOUR
    } else if (n < -(1L << 31) || n >= (1L << 31)) {
      output64(chan, CODE_INT64, n);
#endif
    } else
      output32(chan, CODE_INT32, n);
  } else {
    header_t hd = Hd_val(v);
    tag_t tag = Tag_hd(hd);
    mlsize_t sz = Wosize_hd(hd);
      asize_t h;
    /* Atoms are treated specially for two reasons: they are not allocated
       in the externed block, and they are automatically shared. */
    if (sz == 0) {
      if (tag < 16) {
        putch(chan, PREFIX_SMALL_BLOCK + tag);
      } else {
        output32(chan, CODE_BLOCK32, hd);
      }
    } else {
      /* Check if already seen */
      if (2 * obj_counter >= extern_table_size) resize_extern_table();
      h = Hash(v);
      while (extern_table[h].obj != 0) {
        if (extern_table[h].obj == v) {
          byteoffset_t d = obj_counter - extern_table[h].ofs;
          if (d < 0x100) {
            output8(chan, CODE_SHARED8, d);
          } else if (d < 0x10000) {
            output16(chan, CODE_SHARED16, d);
          } else {
            output32(chan, CODE_SHARED32, d);
          }
          return;
        }
        h++;
        if (h >= extern_table_size) h = 0;
      }
      /* Not seen yet. Record the object and output its contents. */
      extern_table[h].obj = v;
      extern_table[h].ofs = obj_counter;
      obj_counter++;
      switch(tag) {
      case String_tag: {
        mlsize_t len = string_length(v);
        if (len < 0x20) {
          putch(chan, PREFIX_SMALL_STRING + len);
        } else if (len < 0x100) {
          output8(chan, CODE_STRING8, len);
        } else {
          output32(chan, CODE_STRING32, len);
        }
        putblock(chan, String_val(v), len);
        size_32 += 1 + (len + 4) / 4;
        size_64 += 1 + (len + 8) / 8;
        break;
      }
      case Double_tag: {
        double buffer;
        if (sizeof(double) != 8)
          invalid_argument("output_value: non-standard floats");
        putch(chan, CODE_DOUBLE_NATIVE);
        buffer = Double_val(v);
        putblock(chan, (char *) &buffer, 8);
        size_32 += 1 + sizeof(double) / 4;
        size_64 += 1 + sizeof(double) / 8;
        break;
      }
      case Abstract_tag:
      case Final_tag:
        invalid_argument("output_value: abstract value");
        break;
      case Closure_tag:
      case Infix_tag:
        invalid_argument("output_value: functional value");
        break;
      default: {
        mlsize_t i;
        if (tag < 16 && sz < 8) {
          putch(chan, PREFIX_SMALL_BLOCK + tag + (sz << 4));
        } else {
          output32(chan, CODE_BLOCK32, hd);
        }
        size_32 += 1 + sz;
        size_64 += 1 + sz;
        for (i = 0; i < sz - 1; i++) emit_compact(chan, Field(v, i));
        v = Field(v, i);
        goto tailcall;
      }
      }
    }
  }
}

value output_value(chan, v) /* ML */
     struct channel * chan;
     value v;
{
  value start_loc, final_loc;
  putword(chan, Compact_magic_number);
  start_loc = pos_out(chan);
  putword(chan, 0);
  putword(chan, 0);
  putword(chan, 0);
  extern_table_size = INITIAL_EXTERN_TABLE_SIZE;
  alloc_extern_table();
  obj_counter = 0;
  size_32 = 0;
  size_64 = 0;
  emit_compact(chan, v);
#ifdef SIXTYFOUR
  if (size_32 >= (1L << 32) || size_64 >= (1L << 32)) {
    /* The object is so big its size cannot be written in the header.
       Besides, some of the block sizes or string lengths or shared offsets
       it contains may have overflowed the 32 bits used to write them. */
    failwith("output_value: object too big");
  }
#endif
  final_loc = pos_out(chan);
  seek_out(chan, start_loc);
  putword(chan, obj_counter);
  putword(chan, size_32);
  putword(chan, size_64);
  seek_out(chan, final_loc);
  stat_free((char *) extern_table);
  return Val_unit;
}