1 /*
2 * Copyright (C) 2012-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 /*
21 * Types
22 */
23 typedef union _jit_thumb_t {
24 int32_t i;
25 int16_t s[2];
26 } jit_thumb_t;
27
28 /* libgcc */
29 extern void __clear_cache(void *, void *);
30
31 #include "arm-cpu.c"
32 #include "arm-vfp.c"
33
34 static const jit_gpr_t abi_gpr_args[] = {
35 _R0, _R1, _R2, _R3
36 };
37 static const int abi_gpr_arg_count = sizeof(abi_gpr_args) / sizeof(abi_gpr_args[0]);
38
39 struct abi_arg_iterator
40 {
41 const jit_operand_t *args;
42 size_t argc;
43
44 size_t arg_idx;
45 size_t gpr_idx;
46 uint32_t vfp_used_registers;
47 size_t stack_size;
48 size_t stack_padding;
49 };
50
51 static size_t page_size;
52
53 jit_bool_t
jit_get_cpu(void)54 jit_get_cpu(void)
55 {
56 page_size = sysconf(_SC_PAGE_SIZE);
57 // FIXME check version, thumb, hardware fp support
58 return 1;
59 }
60
61 jit_bool_t
jit_init(jit_state_t * _jit)62 jit_init(jit_state_t *_jit)
63 {
64 return 1;
65 }
66
67 static size_t
jit_initial_frame_size(void)68 jit_initial_frame_size (void)
69 {
70 return 0;
71 }
72
73 static void
reset_abi_arg_iterator(struct abi_arg_iterator * iter,size_t argc,const jit_operand_t * args)74 reset_abi_arg_iterator(struct abi_arg_iterator *iter, size_t argc,
75 const jit_operand_t *args)
76 {
77 memset(iter, 0, sizeof *iter);
78 iter->argc = argc;
79 iter->args = args;
80 }
81
82 static void
next_abi_arg(struct abi_arg_iterator * iter,jit_operand_t * arg)83 next_abi_arg(struct abi_arg_iterator *iter, jit_operand_t *arg)
84 {
85 ASSERT(iter->arg_idx < iter->argc);
86 enum jit_operand_abi abi = iter->args[iter->arg_idx].abi;
87 iter->arg_idx++;
88 if (is_gpr_arg(abi) && iter->gpr_idx < abi_gpr_arg_count) {
89 *arg = jit_operand_gpr (abi, abi_gpr_args[iter->gpr_idx++]);
90 return;
91 }
92 if (is_fpr_arg(abi)) {
93 // The ARM VFP ABI passes floating-point arguments in d0-d7
94 // (s0-s15), and allows for "back-filling". Say you have a
95 // function:
96 //
97 // void f(float a, double b, float c);
98 //
99 // A gets allocated to s0, then b to d1 (which aliases s2+s3), then
100 // c to s1.
101 uint32_t width = abi == JIT_OPERAND_ABI_FLOAT ? 1 : 2;
102 uint32_t mask = (1 << width) - 1;
103 for (size_t i = 0; i < 16; i += width) {
104 if ((iter->vfp_used_registers & (mask << i)) == 0) {
105 iter->vfp_used_registers |= (mask << i);
106 *arg = jit_operand_fpr (abi, JIT_FPR(i));
107 return;
108 }
109 }
110 }
111 *arg = jit_operand_mem (abi, JIT_SP, iter->stack_size);
112 iter->stack_size += 4;
113 }
114
115 static void
jit_flush(void * fptr,void * tptr)116 jit_flush(void *fptr, void *tptr)
117 {
118 jit_word_t f = (jit_word_t)fptr & -page_size;
119 jit_word_t t = (((jit_word_t)tptr) + page_size - 1) & -page_size;
120 __clear_cache((void *)f, (void *)t);
121 }
122
123 static inline size_t
jit_stack_alignment(void)124 jit_stack_alignment(void)
125 {
126 return 8;
127 }
128
129 static void
jit_try_shorten(jit_state_t * _jit,jit_reloc_t reloc,jit_pointer_t addr)130 jit_try_shorten(jit_state_t *_jit, jit_reloc_t reloc, jit_pointer_t addr)
131 {
132 }
133
134 static void*
bless_function_pointer(void * ptr)135 bless_function_pointer(void *ptr)
136 {
137 // Set low bit to mark as thumb mode.
138 return (void*) (((uintptr_t)ptr) | 1);
139 }
140