| 1 | /* |
| 2 | * Copyright (C) 2013-2019 Free Software Foundation, Inc. |
| 3 | * |
| 4 | * This file is part of GNU lightning. |
| 5 | * |
| 6 | * GNU lightning is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU Lesser General Public License as published |
| 8 | * by the Free Software Foundation; either version 3, or (at your option) |
| 9 | * any later version. |
| 10 | * |
| 11 | * GNU lightning is distributed in the hope that it will be useful, but |
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 13 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
| 14 | * License for more details. |
| 15 | * |
| 16 | * Authors: |
| 17 | * Paulo Cesar Pereira de Andrade |
| 18 | */ |
| 19 | |
| 20 | #include <lightning.h> |
| 21 | #include <lightning/jit_private.h> |
| 22 | #if GET_JIT_SIZE |
| 23 | # include <stdio.h> |
| 24 | #endif |
| 25 | |
| 26 | /* |
| 27 | * Initialization |
| 28 | */ |
| 29 | static jit_int16_t _szs[jit_code_last_code] = { |
| 30 | #if GET_JIT_SIZE |
| 31 | # define JIT_INSTR_MAX 256 |
| 32 | #else |
| 33 | # if defined(__i386__) || defined(__x86_64__) |
| 34 | # include "jit_x86-sz.c" |
| 35 | # elif defined(__mips__) |
| 36 | # include "jit_mips-sz.c" |
| 37 | # elif defined(__arm__) |
| 38 | # include "jit_arm-sz.c" |
| 39 | # elif defined(__powerpc__) |
| 40 | # include "jit_ppc-sz.c" |
| 41 | # elif defined(__sparc__) |
| 42 | # include "jit_sparc-sz.c" |
| 43 | # elif defined(__ia64__) |
| 44 | # include "jit_ia64-sz.c" |
| 45 | # elif defined(__hppa__) |
| 46 | # include "jit_hppa-sz.c" |
| 47 | # elif defined(__aarch64__) |
| 48 | # include "jit_aarch64-sz.c" |
| 49 | # elif defined(__s390__) || defined(__s390x__) |
| 50 | # include "jit_s390-sz.c" |
| 51 | # elif defined(__alpha__) |
| 52 | # include "jit_alpha-sz.c" |
| 53 | # elif defined(__riscv) |
| 54 | # include "jit_riscv-sz.c" |
| 55 | # endif |
| 56 | #endif |
| 57 | }; |
| 58 | |
| 59 | /* |
| 60 | * Implementation |
| 61 | */ |
| 62 | void |
| 63 | jit_init_size(void) |
| 64 | { |
| 65 | #if DEBUG |
| 66 | # if !GET_JIT_SIZE |
| 67 | jit_word_t offset; |
| 68 | |
| 69 | for (offset = 0; offset < jit_size(_szs); offset++) |
| 70 | if (_szs[offset] != 0) |
| 71 | return; |
| 72 | /* Ensure data was collected */ |
| 73 | abort(); |
| 74 | # endif |
| 75 | #endif |
| 76 | } |
| 77 | |
| 78 | #if GET_JIT_SIZE |
| 79 | void |
| 80 | _jit_size_prepare(jit_state_t *_jit) |
| 81 | { |
| 82 | _jitc->cptr = _jit->code.ptr; |
| 83 | _jitc->size = _jit->pc.w; |
| 84 | } |
| 85 | |
| 86 | void |
| 87 | _jit_size_collect(jit_state_t *_jit, jit_node_t *node) |
| 88 | { |
| 89 | jit_word_t length; |
| 90 | |
| 91 | if (_jitc->cptr == _jit->code.ptr) { |
| 92 | length = _jit->pc.w - _jitc->size; |
| 93 | if (_szs[node->code] < length) |
| 94 | _szs[node->code] = length; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | #else |
| 99 | jit_word_t |
| 100 | _jit_get_size(jit_state_t *_jit) |
| 101 | { |
| 102 | jit_word_t size; |
| 103 | jit_node_t *node; |
| 104 | |
| 105 | for (size = JIT_INSTR_MAX, node = _jitc->head; node; node = node->next) |
| 106 | size += _szs[node->code]; |
| 107 | |
| 108 | return size; |
| 109 | } |
| 110 | #endif |
| 111 | |
| 112 | jit_word_t |
| 113 | jit_get_max_instr(void) |
| 114 | { |
| 115 | return (JIT_INSTR_MAX >= 144 ? JIT_INSTR_MAX : 144); |
| 116 | } |
| 117 | |
| 118 | void |
| 119 | jit_finish_size(void) |
| 120 | { |
| 121 | #if GET_JIT_SIZE |
| 122 | FILE *fp; |
| 123 | jit_word_t offset; |
| 124 | |
| 125 | /* Define a single path */ |
| 126 | fp = fopen(JIT_SIZE_PATH, "a"); |
| 127 | assert(fp); |
| 128 | for (offset = 0; offset < jit_size(_szs); offset++) |
| 129 | fprintf(fp, "%d %d\n", offset, _szs[offset]); |
| 130 | fclose(fp); |
| 131 | #endif |
| 132 | } |