1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-05 16:26:17 +00:00
janet/src/core/compile.h

262 lines
7.5 KiB
C
Raw Normal View History

2017-12-15 00:33:45 +00:00
/*
2021-05-31 18:46:02 +00:00
* Copyright (c) 2021 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
#ifndef JANET_AMALG
#include "features.h"
#include <janet.h>
2018-07-01 15:52:15 +00:00
#include "regalloc.h"
#endif
2017-12-15 00:33:45 +00:00
/* Levels for compiler warnings */
typedef enum {
JANET_C_LINT_RELAXED,
JANET_C_LINT_NORMAL,
JANET_C_LINT_STRICT
} JanetCompileLintLevel;
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_IN 6
2018-09-06 02:18:42 +00:00
#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_GT 20
#define JANET_FUN_LT 21
#define JANET_FUN_GTE 22
#define JANET_FUN_LTE 23
#define JANET_FUN_EQ 24
#define JANET_FUN_NEQ 25
#define JANET_FUN_PROP 26
#define JANET_FUN_GET 27
#define JANET_FUN_NEXT 28
#define JANET_FUN_MODULO 29
#define JANET_FUN_REMAINDER 30
#define JANET_FUN_CMP 31
#define JANET_FUN_CANCEL 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
#define JANET_SLOT_DEP_NOTE 0x200000
#define JANET_SLOT_DEP_WARN 0x400000
#define JANET_SLOT_DEP_ERROR 0x800000
#define JANET_SLOT_SPLICED 0x1000000
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-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
#define JANET_SCOPE_WHILE 32
2017-12-15 00:33:45 +00:00
/* A symbol and slot pair */
typedef struct SymPair {
2018-09-06 02:18:42 +00:00
JanetSlot slot;
const uint8_t *sym;
2018-01-21 19:39:32 +00:00
int keep;
} 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
/* Constants for this funcdef */
2018-09-06 02:18:42 +00:00
Janet *consts;
/* Map of symbols to slots. Use a simple linear scan for symbols. */
SymPair *syms;
/* FuncDefs */
2018-09-06 02:18:42 +00:00
JanetFuncDef **defs;
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
/* Upvalue allocator */
JanetcRegisterAllocator ua;
/* Referenced closure environments. The values at each index correspond
* 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;
int32_t bytecode_start;
int flags;
};
2017-12-15 00:33:45 +00:00
/* Compilation state */
2018-09-06 02:18:42 +00:00
struct JanetCompiler {
2018-07-01 15:52:15 +00:00
/* Pointer to current scope */
2018-09-06 02:18:42 +00:00
JanetScope *scope;
uint32_t *buffer;
2018-09-06 02:18:42 +00:00
JanetSourceMapping *mapbuffer;
2018-06-29 03:36:31 +00:00
/* Hold the environment */
2018-09-06 02:18:42 +00:00
JanetTable *env;
/* 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;
/* Collect linting results */
JanetArray *lints;
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
/* 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
};
/* Get the default form options */
2018-09-06 02:18:42 +00:00
JanetFopts janetc_fopts_default(JanetCompiler *c);
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);
JanetSlot(*optimize)(JanetFopts opts, JanetSlot *args);
2018-07-01 23:35:45 +00:00
};
/* A grouping of a named special and the corresponding compiler fragment */
2018-09-06 02:18:42 +00:00
struct JanetSpecial {
const char *name;
JanetSlot(*compile)(JanetFopts opts, int32_t argn, const Janet *argv);
};
2017-12-15 00:33:45 +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
/* 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-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);
/* 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);
/* 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);
/* 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);
/* Get a bunch of slots for function arguments */
2018-09-06 02:18:42 +00:00
JanetSlot *janetc_toslotskv(JanetCompiler *c, Janet ds);
2018-09-06 02:18:42 +00:00
/* Push slots load via janetc_toslots. */
int32_t janetc_pushslots(JanetCompiler *c, JanetSlot *slots);
2018-09-06 02:18:42 +00:00
/* Free slots loaded via janetc_toslots */
void janetc_freeslots(JanetCompiler *c, JanetSlot *slots);
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
/* 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
/* Linting */
void janetc_lintf(JanetCompiler *C, JanetCompileLintLevel level, const char *format, ...);
/* Dispatch to correct form compiler */
2018-09-06 02:18:42 +00:00
JanetSlot janetc_value(JanetFopts opts, Janet x);
/* 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
/* Create a destory slots */
2018-09-06 02:18:42 +00:00
JanetSlot janetc_cslot(Janet x);
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