Commit | Line | Data |
---|---|---|
4a71579b | 1 | /* |
c0c16242 | 2 | * Copyright (C) 2013-2022 Free Software Foundation, Inc. |
4a71579b PC |
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 | |
c0c16242 | 31 | # define JIT_INSTR_MAX 512 |
4a71579b PC |
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; | |
c0c16242 PC |
104 | # if __riscv && __WORDSIZE == 64 |
105 | jit_word_t extra = 0; | |
106 | # endif | |
4a71579b | 107 | |
c0c16242 PC |
108 | for (size = JIT_INSTR_MAX, node = _jitc->head; node; node = node->next) { |
109 | # if __riscv && __WORDSIZE == 64 | |
110 | /* Get estimative of extra memory for constants at end of code. */ | |
111 | switch (node->code) { | |
112 | case jit_code_movi: | |
113 | case jit_code_movi_f: | |
114 | case jit_code_movi_d: | |
115 | case jit_code_jmpi: | |
116 | case jit_code_calli: | |
117 | extra += sizeof(jit_word_t); | |
118 | default: | |
119 | break; | |
120 | } | |
121 | # endif | |
4a71579b | 122 | size += _szs[node->code]; |
c0c16242 PC |
123 | } |
124 | # if __riscv && __WORDSIZE == 64 | |
125 | /* Heuristically only 20% of constants are unique. */ | |
126 | size += extra / 5; | |
127 | # endif | |
4a71579b | 128 | |
b94eaa6f | 129 | return size; |
4a71579b PC |
130 | } |
131 | #endif | |
132 | ||
133 | jit_word_t | |
134 | jit_get_max_instr(void) | |
135 | { | |
136 | return (JIT_INSTR_MAX >= 144 ? JIT_INSTR_MAX : 144); | |
137 | } | |
138 | ||
139 | void | |
140 | jit_finish_size(void) | |
141 | { | |
142 | #if GET_JIT_SIZE | |
143 | FILE *fp; | |
144 | jit_word_t offset; | |
145 | ||
146 | /* Define a single path */ | |
147 | fp = fopen(JIT_SIZE_PATH, "a"); | |
148 | assert(fp); | |
149 | for (offset = 0; offset < jit_size(_szs); offset++) | |
150 | fprintf(fp, "%d %d\n", offset, _szs[offset]); | |
151 | fclose(fp); | |
152 | #endif | |
153 | } |