kit

kit
git clone https://git.ryansepassi.com/git/kit.git
Log | Files | Refs | README

inputs.h (5863B)


      1 #ifndef KIT_DRIVER_INPUTS_H
      2 #define KIT_DRIVER_INPUTS_H
      3 
      4 #include <kit/compile.h>
      5 #include <kit/link.h>
      6 #include <kit/object.h>
      7 
      8 #include "driver.h"
      9 
     10 /* Shared input handling for tools that take a mixed list of C sources,
     11  * stdin source, object files, and static archives — `kit run` and
     12  * `kit dbg` today. Owns parallel arrays sized to the worst-case argv
     13  * slot count plus an optional one-shot stdin slurp.
     14  *
     15  * Usage:
     16  *   DriverInputs in = {0};
     17  *   if (driver_inputs_init(&in, env, TOOL, argc) != 0) return 1;
     18  *   for (i = 1; i < argc; ++i) {
     19  *       int r = driver_inputs_classify(&in, argv[i]);
     20  *       if (r < 0) { driver_inputs_release(&in); return 1; }
     21  *       if (r) continue;
     22  *       ... handle other flags / positionals ...
     23  *   }
     24  *   if (driver_inputs_count(&in) == 0) { ... no inputs ... }
     25  *   rc = driver_inputs_compile_and_jit(&in, compiler, host, &copts, &pp, entry,
     26  *                                      driver_dlsym_resolver, NULL, &jit);
     27  *   driver_inputs_release(&in);
     28  *
     29  * Path strings (`.c`, `.o`, `.a`) are borrowed from argv. The stdin
     30  * buffer is heap-owned and freed by driver_inputs_release. */
     31 typedef struct DriverInputs {
     32   DriverEnv* env;
     33   const char* tool;
     34   const KitFrontendRegistry* frontends; /* borrowed; NULL means builtins */
     35   size_t bound;
     36 
     37   const char** sources; /* .c .cc .cpp paths (borrowed) */
     38   uint32_t nsources;
     39   KitSourceInput* source_memory; /* "-" stdin slurp; at most one entry */
     40   uint32_t nsource_memory;
     41   uint8_t* stdin_buf; /* owning storage for the one stdin slurp */
     42   size_t stdin_size;
     43   const char** object_files; /* .o .obj paths (borrowed) */
     44   uint32_t nobject_files;
     45   const char** archives; /* .a paths (borrowed) */
     46   uint32_t narchives;
     47 } DriverInputs;
     48 
     49 /* Allocate the parallel arrays. `argc` is the worst-case input count
     50  * (typically the program's argc). `tool` is the short name used in
     51  * driver_errf messages. Returns 0 on success, 1 on allocation failure
     52  * (already reported). */
     53 int driver_inputs_init(DriverInputs*, DriverEnv*, const char* tool, int argc);
     54 
     55 /* Release every allocation held by *in, including the stdin buffer.
     56  * Safe to call even after a failed init. */
     57 void driver_inputs_release(DriverInputs*);
     58 
     59 /* Try to consume one positional input at `arg`. Recognized:
     60  *   "-"                read C source from stdin (at most once)
     61  *   *.c *.toy *.s *.wat *.wasm
     62  *                      source path
     63  *   *.o *.obj          object-file path
     64  *   *.a                static-archive path
     65  *
     66  * Returns:
     67  *   1  arg matched and was recorded.
     68  *   0  arg is not a recognized input shape (caller decides whether to
     69  *      treat it as an unknown flag, etc.).
     70  *  -1  arg matched a recognized shape but recording failed (duplicate
     71  *      stdin, stdin read failure, allocation failure); the error has
     72  *      already been reported via driver_errf. */
     73 int driver_inputs_classify(DriverInputs*, const char* arg);
     74 
     75 /* Total recorded inputs across every kind. */
     76 uint32_t driver_inputs_count(const DriverInputs*);
     77 
     78 /* Display name of the first recorded input (sources, then source_memory,
     79  * then object_files, then archives — matches the order used by the
     80  * link/compile loop). Used by callers to synthesize argv[0] for the
     81  * JITed program. Returns NULL when no inputs are recorded. */
     82 const char* driver_inputs_first_name(const DriverInputs*);
     83 
     84 /* Load every input through env.file_io, compile sources via `compiler`
     85  * with `copts` (code/diagnostics) and `pp` (preprocessor settings, applied to
     86  * preprocessor-enabled frontends), and JIT-link the result. `host` carries
     87  * execmem/tls; `extern_resolver` populates the JIT link options. `pp` may be
     88  * NULL for no preprocessor configuration. On success *out_jit owns the JIT
     89  * image; on failure an error has already been reported. Returns 0 on success,
     90  * 1 otherwise. */
     91 int driver_inputs_compile_and_jit(DriverInputs*, KitCompiler*,
     92                                   const KitJitHost*,
     93                                   const KitCCompileOptions* copts,
     94                                   const KitPreprocessOptions* pp,
     95                                   const char* entry,
     96                                   void* (*extern_resolver)(void*, KitSlice),
     97                                   void* extern_resolver_user, KitJit** out_jit);
     98 
     99 /* ----------------------------------------------------------------------
    100  * Per-object global-symbol collection
    101  *
    102  * ar / ranlib / strip all need to enumerate the globally-defined symbols
    103  * of an object file so the archive symbol index (ar/ranlib) or the
    104  * --strip-unneeded keep-set (strip) can be populated. The shape is
    105  * always the same: open the object, walk symbols, copy out the names
    106  * of every SB_GLOBAL symbol with a defining section.
    107  *
    108  * driver_collect_obj_global_syms allocates a single heap block laid out
    109  * as [KitSlice names[count]][NUL-separated name bytes]. The caller
    110  * frees the block via driver_collect_obj_global_syms_free. Each name
    111  * slice points into the name-byte region and is NUL-terminated there.
    112  *
    113  * Returns:
    114  *   0  member parsed, output filled (count may be 0 if the object has
    115  *      no globally-defined symbols, or the member is not a recognized
    116  *      object file at all — in that case *blob_out is NULL).
    117  *   1  fatal failure (out of memory, etc.); an error has been reported
    118  *      via driver_errf using the supplied tool tag.
    119  */
    120 int driver_collect_obj_global_syms(DriverEnv* env, const KitContext* ctx,
    121                                    const char* tool, const KitSlice* member,
    122                                    void** blob_out, size_t* blob_size_out,
    123                                    const KitSlice** names_out,
    124                                    uint32_t* count_out);
    125 
    126 void driver_collect_obj_global_syms_free(DriverEnv* env, void* blob,
    127                                          size_t blob_size);
    128 
    129 #endif