From 47e015e9468ce109c0fb28562d2b768ba29d2de3 Mon Sep 17 00:00:00 2001 From: Stephen Dolan Date: Sun, 5 May 2013 22:37:46 +0100 Subject: Make a few constant globals actually const. This means the .data and .bss sections are empty, a good thing in a shared library (see #98). --- builtin.c | 10 +++------- compile.c | 8 ++++---- compile.h | 3 ++- jv_parse.c | 4 ++-- jv_print.c | 4 ++-- 5 files changed, 13 insertions(+), 16 deletions(-) diff --git a/builtin.c b/builtin.c index 76e559ea..b9c60b83 100644 --- a/builtin.c +++ b/builtin.c @@ -1,6 +1,5 @@ #include #include "builtin.h" -#include "bytecode.h" #include "compile.h" #include "parser.h" #include "locfile.h" @@ -469,7 +468,7 @@ static jv f_error(jv input, jv msg) { return jv_invalid_with_msg(msg); } -static struct cfunction function_list[] = { +static const struct cfunction function_list[] = { {(cfunction_ptr)f_plus, "_plus", 3}, {(cfunction_ptr)f_negate, "_negate", 1}, {(cfunction_ptr)f_minus, "_minus", 3}, @@ -501,9 +500,6 @@ static struct cfunction function_list[] = { {(cfunction_ptr)f_format, "format", 2}, }; -static struct symbol_table cbuiltins = - {function_list, sizeof(function_list)/sizeof(function_list[0])}; - struct bytecoded_builtin { const char* name; block code; }; static block bind_bytecoded_builtins(block b) { block builtins = gen_noop(); @@ -537,7 +533,7 @@ static block bind_bytecoded_builtins(block b) { return block_bind(builtins, b, OP_IS_CALL_PSEUDO); } -static const char* jq_builtins[] = { +static const char* const jq_builtins[] = { "def map(f): [.[] | f];", "def select(f): if f then . else empty end;", "def sort_by(f): _sort_by_impl(map([f]));", @@ -564,5 +560,5 @@ block builtins_bind(block b) { locfile_free(&src); } b = bind_bytecoded_builtins(b); - return gen_cbinding(&cbuiltins, b); + return gen_cbinding(function_list, sizeof(function_list)/sizeof(function_list[0]), b); } diff --git a/compile.c b/compile.c index b06e4f75..69044626 100644 --- a/compile.c +++ b/compile.c @@ -30,7 +30,7 @@ struct inst { uint16_t intval; struct inst* target; jv constant; - struct cfunction* cfunc; + const struct cfunction* cfunc; } imm; location source; @@ -377,10 +377,10 @@ block gen_cond(block cond, block iftrue, block iffalse) { BLOCK(gen_op_simple(POP), iffalse))); } -block gen_cbinding(struct symbol_table* t, block code) { - for (int cfunc=0; cfuncncfunctions; cfunc++) { +block gen_cbinding(const struct cfunction* cfunctions, int ncfunctions, block code) { + for (int cfunc=0; cfuncimm.cfunc = &t->cfunctions[cfunc]; + i->imm.cfunc = &cfunctions[cfunc]; i->symbol = strdup(i->imm.cfunc->name); code = block_bind(inst_block(i), code, OP_IS_CALL_PSEUDO); } diff --git a/compile.h b/compile.h index 150eef28..f982e3bf 100644 --- a/compile.h +++ b/compile.h @@ -7,6 +7,7 @@ struct bytecode; struct symbol_table; +struct cfunction; struct inst; typedef struct inst inst; @@ -41,7 +42,7 @@ block gen_or(block a, block b); block gen_cond(block cond, block iftrue, block iffalse); -block gen_cbinding(struct symbol_table* functions, block b); +block gen_cbinding(const struct cfunction* functions, int nfunctions, block b); void block_append(block* b, block b2); block block_join(block a, block b); diff --git a/jv_parse.c b/jv_parse.c index f55c17e5..a8617e06 100644 --- a/jv_parse.c +++ b/jv_parse.c @@ -283,7 +283,7 @@ static chclass classify(char c) { } -static presult OK = "output produced"; +static const presult OK = "output produced"; static int check_done(struct jv_parser* p, jv* out) { if (p->stackpos == 0 && jv_is_valid(p->next)) { @@ -341,7 +341,7 @@ static pfunc scan(struct jv_parser* p, char ch, jv* out) { return answer; } -static unsigned char UTF8_BOM[] = {0xEF,0xBB,0xBF}; +static const unsigned char UTF8_BOM[] = {0xEF,0xBB,0xBF}; void jv_parser_set_buf(struct jv_parser* p, const char* buf, int length, int is_partial) { assert((p->curr_buf == 0 || p->curr_buf_pos == p->curr_buf_length) diff --git a/jv_print.c b/jv_print.c index 388bf663..6396d7c8 100644 --- a/jv_print.c +++ b/jv_print.c @@ -12,10 +12,10 @@ // Colour table. See http://en.wikipedia.org/wiki/ANSI_escape_code#Colors // for how to choose these. -static jv_kind colour_kinds[] = +static const jv_kind colour_kinds[] = {JV_KIND_NULL, JV_KIND_FALSE, JV_KIND_TRUE, JV_KIND_NUMBER, JV_KIND_STRING, JV_KIND_ARRAY, JV_KIND_OBJECT}; -static const char* colours[] = +static const char* const colours[] = {COL("30;1"), COL("0"), COL("0"), COL("0"), COL("32"), COL("37"), COL("37")}; #define FIELD_COLOUR COL("34;1") -- cgit v1.2.3