plum-umd / the-838e-compiler

Compiler for CMSC 838E
2 stars 0 forks source link

C API for Villain #56

Closed 8tx7K38ej1aBTKWK closed 3 years ago

8tx7K38ej1aBTKWK commented 3 years ago

This implements the C API part of #43. It simplifies the code base quite a lot so I'm sending a preliminary pull request.

The details of the API can be found in villain.h and wrap.c.

I also changed the vector representation a little bit so it matches the C API

typedef struct vl_vec {
  uint64_t len;
  vl_val buf[];
} vl_vec;

In particular, len is no longer a tagged value so unwrapping the vector becomes a simple bit-untagging

vl_vec* vl_unwrap_vec(vl_val x)
{
  return (vl_vec *)(x ^ vector_type_tag);
}

The next step is to redo the string representation so it matches the definition in C

typedef struct vl_str {
  uint64_t len;
  vl_char buf[];
} vl_str;

We need to

  1. use untagged value for len
  2. use UTF32 for buf. Each character should also be untagged.

Tagging should only be done when string-ref or string-length are called.

After the next step, unwrapping a string can be much simpler

vl_str* vl_unwrap_str(vl_val x)
{
  return (vl_str *)(x ^ str_type_tag);
}
vl_val vl_wrap_str(vl_str *s)
{
  return ((vl_val)s) | str_type_tag;
}
8tx7K38ej1aBTKWK commented 3 years ago

I have resolved all the merging conflicts and added API for ports

oops :)