2017-12-15 00:33:45 +00:00
|
|
|
/*
|
2019-01-06 08:23:03 +00:00
|
|
|
* Copyright (c) 2019 Calvin Rose
|
2017-12-15 00:33:45 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
#ifndef JANET_COMPILE_H
|
|
|
|
#define JANET_COMPILE_H
|
2017-12-15 00:33:45 +00:00
|
|
|
|
2019-01-24 05:15:58 +00:00
|
|
|
#ifndef JANET_AMALG
|
2019-02-19 01:13:35 +00:00
|
|
|
#include <janet.h>
|
2018-07-01 15:52:15 +00:00
|
|
|
#include "regalloc.h"
|
2019-01-24 05:15:58 +00:00
|
|
|
#endif
|
2017-12-15 00:33:45 +00:00
|
|
|
|
2018-07-01 23:35:45 +00:00
|
|
|
/* Tags for some functions for the prepared inliner */
|
2018-09-06 02:18:42 +00:00
|
|
|
#define JANET_FUN_DEBUG 1
|
|
|
|
#define JANET_FUN_ERROR 2
|
|
|
|
#define JANET_FUN_APPLY 3
|
|
|
|
#define JANET_FUN_YIELD 4
|
|
|
|
#define JANET_FUN_RESUME 5
|
|
|
|
#define JANET_FUN_GET 6
|
|
|
|
#define JANET_FUN_PUT 7
|
|
|
|
#define JANET_FUN_LENGTH 8
|
|
|
|
#define JANET_FUN_ADD 9
|
|
|
|
#define JANET_FUN_SUBTRACT 10
|
|
|
|
#define JANET_FUN_MULTIPLY 11
|
|
|
|
#define JANET_FUN_DIVIDE 12
|
|
|
|
#define JANET_FUN_BAND 13
|
|
|
|
#define JANET_FUN_BOR 14
|
|
|
|
#define JANET_FUN_BXOR 15
|
|
|
|
#define JANET_FUN_LSHIFT 16
|
|
|
|
#define JANET_FUN_RSHIFT 17
|
|
|
|
#define JANET_FUN_RSHIFTU 18
|
|
|
|
#define JANET_FUN_BNOT 19
|
|
|
|
#define JANET_FUN_ORDER_GT 20
|
|
|
|
#define JANET_FUN_ORDER_LT 21
|
|
|
|
#define JANET_FUN_ORDER_GTE 22
|
2018-11-16 21:24:10 +00:00
|
|
|
#define JANET_FUN_ORDER_LTE 23
|
|
|
|
#define JANET_FUN_ORDER_EQ 24
|
2018-09-06 02:18:42 +00:00
|
|
|
#define JANET_FUN_ORDER_NEQ 25
|
2018-11-16 21:24:10 +00:00
|
|
|
#define JANET_FUN_GT 26
|
|
|
|
#define JANET_FUN_LT 27
|
|
|
|
#define JANET_FUN_GTE 28
|
|
|
|
#define JANET_FUN_LTE 29
|
|
|
|
#define JANET_FUN_EQ 30
|
2018-09-06 02:18:42 +00:00
|
|
|
#define JANET_FUN_NEQ 31
|
2019-06-24 16:44:13 +00:00
|
|
|
#define JANET_FUN_PROP 32
|
2018-07-01 23:35:45 +00:00
|
|
|
|
2017-12-15 00:33:45 +00:00
|
|
|
/* Compiler typedefs */
|
2018-09-06 02:18:42 +00:00
|
|
|
typedef struct JanetCompiler JanetCompiler;
|
2017-12-15 00:33:45 +00:00
|
|
|
typedef struct FormOptions FormOptions;
|
|
|
|
typedef struct SlotTracker SlotTracker;
|
2018-09-06 02:18:42 +00:00
|
|
|
typedef struct JanetScope JanetScope;
|
|
|
|
typedef struct JanetSlot JanetSlot;
|
|
|
|
typedef struct JanetFopts JanetFopts;
|
|
|
|
typedef struct JanetFunOptimizer JanetFunOptimizer;
|
|
|
|
typedef struct JanetSpecial JanetSpecial;
|
|
|
|
|
|
|
|
#define JANET_SLOT_CONSTANT 0x10000
|
|
|
|
#define JANET_SLOT_NAMED 0x20000
|
|
|
|
#define JANET_SLOT_MUTABLE 0x40000
|
|
|
|
#define JANET_SLOT_REF 0x80000
|
|
|
|
#define JANET_SLOT_RETURNED 0x100000
|
2017-12-21 04:03:34 +00:00
|
|
|
/* Needed for handling single element arrays as global vars. */
|
2017-12-15 00:33:45 +00:00
|
|
|
|
2018-12-01 03:49:21 +00:00
|
|
|
/* Used for unquote-splicing */
|
|
|
|
#define JANET_SLOT_SPLICED 0x200000
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
#define JANET_SLOTTYPE_ANY 0xFFFF
|
2017-12-15 00:33:45 +00:00
|
|
|
|
|
|
|
/* A stack slot */
|
2018-09-06 02:18:42 +00:00
|
|
|
struct JanetSlot {
|
|
|
|
Janet constant; /* If the slot has a constant value */
|
2017-12-15 00:33:45 +00:00
|
|
|
int32_t index;
|
|
|
|
int32_t envindex; /* 0 is local, positive number is an upvalue */
|
|
|
|
uint32_t flags;
|
2017-12-16 06:17:53 +00:00
|
|
|
};
|
2017-12-15 00:33:45 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
#define JANET_SCOPE_FUNCTION 1
|
|
|
|
#define JANET_SCOPE_ENV 2
|
|
|
|
#define JANET_SCOPE_TOP 4
|
|
|
|
#define JANET_SCOPE_UNUSED 8
|
|
|
|
#define JANET_SCOPE_CLOSURE 16
|
2019-03-09 21:47:05 +00:00
|
|
|
#define JANET_SCOPE_WHILE 32
|
2017-12-15 00:33:45 +00:00
|
|
|
|
2018-01-05 21:17:55 +00:00
|
|
|
/* A symbol and slot pair */
|
|
|
|
typedef struct SymPair {
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetSlot slot;
|
2018-01-05 21:17:55 +00:00
|
|
|
const uint8_t *sym;
|
2018-01-21 19:39:32 +00:00
|
|
|
int keep;
|
2018-01-05 21:17:55 +00:00
|
|
|
} SymPair;
|
|
|
|
|
2017-12-15 00:33:45 +00:00
|
|
|
/* A lexical scope during compilation */
|
2018-09-06 02:18:42 +00:00
|
|
|
struct JanetScope {
|
2017-12-15 00:33:45 +00:00
|
|
|
|
2018-07-01 15:52:15 +00:00
|
|
|
/* For debugging */
|
|
|
|
const char *name;
|
|
|
|
|
|
|
|
/* Scopes are doubly linked list */
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetScope *parent;
|
|
|
|
JanetScope *child;
|
2018-07-01 15:52:15 +00:00
|
|
|
|
2017-12-21 04:03:34 +00:00
|
|
|
/* Constants for this funcdef */
|
2018-09-06 02:18:42 +00:00
|
|
|
Janet *consts;
|
2017-12-21 04:03:34 +00:00
|
|
|
|
|
|
|
/* Map of symbols to slots. Use a simple linear scan for symbols. */
|
2018-01-05 21:17:55 +00:00
|
|
|
SymPair *syms;
|
2017-12-21 04:03:34 +00:00
|
|
|
|
2018-01-04 02:36:10 +00:00
|
|
|
/* FuncDefs */
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetFuncDef **defs;
|
2018-01-04 02:36:10 +00:00
|
|
|
|
2018-07-09 00:54:41 +00:00
|
|
|
/* Regsiter allocator */
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetcRegisterAllocator ra;
|
2018-07-09 00:54:41 +00:00
|
|
|
|
2017-12-16 06:17:53 +00:00
|
|
|
/* Referenced closure environents. The values at each index correspond
|
2018-01-05 21:17:55 +00:00
|
|
|
* to which index to get the environment from in the parent. The environment
|
2017-12-15 00:33:45 +00:00
|
|
|
* that corresponds to the direct parent's stack will always have value 0. */
|
|
|
|
int32_t *envs;
|
|
|
|
|
2017-12-16 06:17:53 +00:00
|
|
|
int32_t bytecode_start;
|
2017-12-30 21:46:59 +00:00
|
|
|
int flags;
|
2017-12-16 06:17:53 +00:00
|
|
|
};
|
|
|
|
|
2017-12-15 00:33:45 +00:00
|
|
|
/* Compilation state */
|
2018-09-06 02:18:42 +00:00
|
|
|
struct JanetCompiler {
|
2018-11-16 21:24:10 +00:00
|
|
|
|
2018-07-01 15:52:15 +00:00
|
|
|
/* Pointer to current scope */
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetScope *scope;
|
2018-05-20 01:16:00 +00:00
|
|
|
|
2017-12-16 06:17:53 +00:00
|
|
|
uint32_t *buffer;
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetSourceMapping *mapbuffer;
|
2018-06-29 03:36:31 +00:00
|
|
|
|
2017-12-21 04:03:34 +00:00
|
|
|
/* Hold the environment */
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetTable *env;
|
2017-12-21 04:03:34 +00:00
|
|
|
|
2018-06-29 14:37:50 +00:00
|
|
|
/* Name of source to attach to generated functions */
|
|
|
|
const uint8_t *source;
|
2018-06-29 03:36:31 +00:00
|
|
|
|
2018-07-09 00:54:41 +00:00
|
|
|
/* The result of compilation */
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetCompileResult result;
|
2018-07-09 00:54:41 +00:00
|
|
|
|
|
|
|
/* Keep track of where we are in the source */
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetSourceMapping current_mapping;
|
2018-07-09 00:54:41 +00:00
|
|
|
|
|
|
|
/* Prevent unbounded recursion */
|
|
|
|
int recursion_guard;
|
2017-12-15 00:33:45 +00:00
|
|
|
};
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
#define JANET_FOPTS_TAIL 0x10000
|
|
|
|
#define JANET_FOPTS_HINT 0x20000
|
|
|
|
#define JANET_FOPTS_DROP 0x40000
|
2017-12-15 00:33:45 +00:00
|
|
|
|
2018-01-04 02:36:10 +00:00
|
|
|
/* Options for compiling a single form */
|
2018-09-06 02:18:42 +00:00
|
|
|
struct JanetFopts {
|
|
|
|
JanetCompiler *compiler;
|
|
|
|
JanetSlot hint;
|
2018-07-09 00:54:41 +00:00
|
|
|
uint32_t flags; /* bit set of accepted primitive types */
|
2017-12-15 00:33:45 +00:00
|
|
|
};
|
|
|
|
|
2018-01-17 04:18:45 +00:00
|
|
|
/* Get the default form options */
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetFopts janetc_fopts_default(JanetCompiler *c);
|
2018-01-17 04:18:45 +00:00
|
|
|
|
2018-07-01 23:35:45 +00:00
|
|
|
/* For optimizing builtin normal functions. */
|
2018-09-06 02:18:42 +00:00
|
|
|
struct JanetFunOptimizer {
|
|
|
|
int (*can_optimize)(JanetFopts opts, JanetSlot *args);
|
2019-02-20 01:51:34 +00:00
|
|
|
JanetSlot(*optimize)(JanetFopts opts, JanetSlot *args);
|
2018-07-01 23:35:45 +00:00
|
|
|
};
|
|
|
|
|
2018-01-12 15:41:27 +00:00
|
|
|
/* A grouping of a named special and the corresponding compiler fragment */
|
2018-09-06 02:18:42 +00:00
|
|
|
struct JanetSpecial {
|
2017-12-16 06:17:53 +00:00
|
|
|
const char *name;
|
2019-02-20 01:51:34 +00:00
|
|
|
JanetSlot(*compile)(JanetFopts opts, int32_t argn, const Janet *argv);
|
2018-01-24 22:59:00 +00:00
|
|
|
};
|
2017-12-15 00:33:45 +00:00
|
|
|
|
2018-01-12 15:41:27 +00:00
|
|
|
/****************************************************/
|
2017-12-15 00:33:45 +00:00
|
|
|
|
2018-07-01 23:35:45 +00:00
|
|
|
/* Get an optimizer if it exists, otherwise NULL */
|
2018-09-06 02:18:42 +00:00
|
|
|
const JanetFunOptimizer *janetc_funopt(uint32_t flags);
|
2017-12-15 00:33:45 +00:00
|
|
|
|
2018-01-12 15:41:27 +00:00
|
|
|
/* Get a special. Return NULL if none exists */
|
2018-09-06 02:18:42 +00:00
|
|
|
const JanetSpecial *janetc_special(const uint8_t *name);
|
2018-01-12 15:41:27 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
void janetc_freeslot(JanetCompiler *c, JanetSlot s);
|
|
|
|
void janetc_nameslot(JanetCompiler *c, const uint8_t *sym, JanetSlot s);
|
|
|
|
JanetSlot janetc_farslot(JanetCompiler *c);
|
2018-01-12 15:41:27 +00:00
|
|
|
|
|
|
|
/* Throw away some code after checking that it is well formed. */
|
2018-09-06 02:18:42 +00:00
|
|
|
void janetc_throwaway(JanetFopts opts, Janet x);
|
2018-01-12 15:41:27 +00:00
|
|
|
|
|
|
|
/* Get a target slot for emitting an instruction. Will always return
|
|
|
|
* a local slot. */
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetSlot janetc_gettarget(JanetFopts opts);
|
2018-01-12 15:41:27 +00:00
|
|
|
|
|
|
|
/* Get a bunch of slots for function arguments */
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetSlot *janetc_toslots(JanetCompiler *c, const Janet *vals, int32_t len);
|
2018-01-12 15:41:27 +00:00
|
|
|
|
|
|
|
/* Get a bunch of slots for function arguments */
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetSlot *janetc_toslotskv(JanetCompiler *c, Janet ds);
|
2018-01-12 15:41:27 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
/* Push slots load via janetc_toslots. */
|
|
|
|
void janetc_pushslots(JanetCompiler *c, JanetSlot *slots);
|
2018-01-12 15:41:27 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
/* Free slots loaded via janetc_toslots */
|
|
|
|
void janetc_freeslots(JanetCompiler *c, JanetSlot *slots);
|
2018-01-12 15:41:27 +00:00
|
|
|
|
2018-07-01 15:52:15 +00:00
|
|
|
/* Generate the return instruction for a slot. */
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetSlot janetc_return(JanetCompiler *c, JanetSlot s);
|
2018-07-01 15:52:15 +00:00
|
|
|
|
2018-01-12 15:41:27 +00:00
|
|
|
/* Store an error */
|
2018-09-06 02:18:42 +00:00
|
|
|
void janetc_error(JanetCompiler *c, const uint8_t *m);
|
|
|
|
void janetc_cerror(JanetCompiler *c, const char *m);
|
2017-12-15 00:33:45 +00:00
|
|
|
|
2018-01-12 15:41:27 +00:00
|
|
|
/* Dispatch to correct form compiler */
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetSlot janetc_value(JanetFopts opts, Janet x);
|
2018-01-12 15:41:27 +00:00
|
|
|
|
|
|
|
/* Push and pop from the scope stack */
|
2018-09-06 02:18:42 +00:00
|
|
|
void janetc_scope(JanetScope *s, JanetCompiler *c, int flags, const char *name);
|
|
|
|
void janetc_popscope(JanetCompiler *c);
|
|
|
|
void janetc_popscope_keepslot(JanetCompiler *c, JanetSlot retslot);
|
|
|
|
JanetFuncDef *janetc_pop_funcdef(JanetCompiler *c);
|
2017-12-15 00:33:45 +00:00
|
|
|
|
2018-01-12 15:41:27 +00:00
|
|
|
/* Create a destory slots */
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetSlot janetc_cslot(Janet x);
|
2018-01-12 15:41:27 +00:00
|
|
|
|
2017-12-15 00:33:45 +00:00
|
|
|
/* Search for a symbol */
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetSlot janetc_resolve(JanetCompiler *c, const uint8_t *sym);
|
2017-12-15 00:33:45 +00:00
|
|
|
|
|
|
|
#endif
|