2017-06-26 09:53:06 +00:00
|
|
|
From ded14c206a5e73ba5eae6d84ed4ee82c11ce6245 Mon Sep 17 00:00:00 2001
|
2016-11-22 22:21:33 +00:00
|
|
|
From: Nicolas Ojeda Bar <n.oje.bar@gmail.com>
|
|
|
|
Date: Tue, 22 Nov 2016 22:30:35 +0100
|
2017-05-10 11:50:40 +00:00
|
|
|
Subject: [PATCH 8/9] Fix immediates' range when adjusting/indexing sp
|
2016-11-22 22:21:33 +00:00
|
|
|
|
|
|
|
---
|
|
|
|
asmcomp/riscv/arch.ml | 3 +++
|
2016-11-23 10:47:58 +00:00
|
|
|
asmcomp/riscv/emit.mlp | 53 ++++++++++++++++++++++++++++++++++------------
|
2016-11-22 22:21:33 +00:00
|
|
|
asmcomp/riscv/selection.ml | 2 +-
|
2016-11-23 10:47:58 +00:00
|
|
|
3 files changed, 44 insertions(+), 14 deletions(-)
|
2016-11-22 22:21:33 +00:00
|
|
|
|
|
|
|
diff --git a/asmcomp/riscv/arch.ml b/asmcomp/riscv/arch.ml
|
2017-05-10 11:50:40 +00:00
|
|
|
index 61a38b1dd..22c807c49 100644
|
2016-11-22 22:21:33 +00:00
|
|
|
--- a/asmcomp/riscv/arch.ml
|
|
|
|
+++ b/asmcomp/riscv/arch.ml
|
|
|
|
@@ -32,6 +32,9 @@ let spacetime_node_hole_pointer_is_live_before = function
|
|
|
|
type addressing_mode =
|
|
|
|
| Iindexed of int (* reg + displ *)
|
|
|
|
|
|
|
|
+let is_immediate n =
|
|
|
|
+ (n <= 2047) && (n >= -2048)
|
|
|
|
+
|
|
|
|
(* Sizes, endianness *)
|
|
|
|
|
|
|
|
let big_endian = false
|
|
|
|
diff --git a/asmcomp/riscv/emit.mlp b/asmcomp/riscv/emit.mlp
|
2017-05-10 11:50:40 +00:00
|
|
|
index 6d0e3aefd..97c49ce80 100644
|
2016-11-22 22:21:33 +00:00
|
|
|
--- a/asmcomp/riscv/emit.mlp
|
|
|
|
+++ b/asmcomp/riscv/emit.mlp
|
2016-11-23 10:47:58 +00:00
|
|
|
@@ -93,6 +93,34 @@ let emit_stack r =
|
2016-11-22 22:21:33 +00:00
|
|
|
let ofs = slot_offset s (register_class r) in `{emit_int ofs}(sp)`
|
|
|
|
| _ -> fatal_error "Emit.emit_stack"
|
|
|
|
|
|
|
|
+(* Adjust sp by the given byte amount *)
|
|
|
|
+
|
|
|
|
+let emit_stack_adjustment = function
|
|
|
|
+ | 0 -> ()
|
|
|
|
+ | n when is_immediate n ->
|
|
|
|
+ ` addi sp, sp, {emit_int n}\n`
|
|
|
|
+ | n ->
|
|
|
|
+ ` li {emit_reg reg_tmp1}, {emit_int n}\n`;
|
|
|
|
+ ` add sp, sp, {emit_reg reg_tmp1}\n`
|
|
|
|
+
|
|
|
|
+let emit_store src ofs =
|
|
|
|
+ if is_immediate ofs then
|
|
|
|
+ ` {emit_string stg} {emit_reg src}, {emit_int ofs}(sp)\n`
|
|
|
|
+ else begin
|
|
|
|
+ ` li {emit_reg reg_tmp1}, {emit_int ofs}\n`;
|
2016-11-23 10:47:58 +00:00
|
|
|
+ ` add {emit_reg reg_tmp1}, sp, {emit_reg reg_tmp1}\n`;
|
2016-11-22 22:21:33 +00:00
|
|
|
+ ` {emit_string stg} {emit_reg src}, 0({emit_reg reg_tmp1})\n`
|
|
|
|
+ end
|
|
|
|
+
|
|
|
|
+let emit_load dst ofs =
|
|
|
|
+ if is_immediate ofs then
|
|
|
|
+ ` {emit_string lg} {emit_reg dst}, {emit_int ofs}(sp)\n`
|
|
|
|
+ else begin
|
|
|
|
+ ` li {emit_reg reg_tmp1}, {emit_int ofs}\n`;
|
2016-11-23 10:47:58 +00:00
|
|
|
+ ` add {emit_reg reg_tmp1}, sp, {emit_reg reg_tmp1}\n`;
|
2016-11-22 22:21:33 +00:00
|
|
|
+ ` {emit_string lg} {emit_reg dst}, 0({emit_reg reg_tmp1})\n`
|
|
|
|
+ end
|
|
|
|
+
|
|
|
|
(* Record live pointers at call points *)
|
|
|
|
|
|
|
|
let record_frame_label ?label live raise_ dbg =
|
2016-11-23 10:47:58 +00:00
|
|
|
@@ -218,6 +246,7 @@ let name_for_specific = function
|
2016-11-22 22:21:33 +00:00
|
|
|
|
|
|
|
(* Name of current function *)
|
|
|
|
let function_name = ref ""
|
|
|
|
+
|
|
|
|
(* Entry point for tail recursive calls *)
|
|
|
|
let tailrec_entry_point = ref 0
|
|
|
|
|
2016-11-23 10:47:58 +00:00
|
|
|
@@ -234,12 +263,14 @@ let emit_instr i =
|
2016-11-22 22:21:33 +00:00
|
|
|
` mv {emit_reg dst}, {emit_reg src}\n`
|
|
|
|
| {loc = Reg _; typ = Float}, {loc = Reg _; typ = Float} ->
|
|
|
|
` fmv.d {emit_reg dst}, {emit_reg src}\n`
|
|
|
|
- | {loc = Reg _; typ = (Val | Int | Addr)}, {loc = Stack _} ->
|
|
|
|
- ` {emit_string stg} {emit_reg src}, {emit_stack dst}\n`
|
|
|
|
+ | {loc = Reg _; typ = (Val | Int | Addr)}, {loc = Stack s} ->
|
|
|
|
+ let ofs = slot_offset s (register_class dst) in
|
|
|
|
+ emit_store src ofs
|
|
|
|
| {loc = Reg _; typ = Float}, {loc = Stack _} ->
|
|
|
|
` fsd {emit_reg src}, {emit_stack dst}\n`
|
|
|
|
- | {loc = Stack _; typ = (Val | Int | Addr)}, {loc = Reg _ } ->
|
|
|
|
- ` {emit_string lg} {emit_reg dst}, {emit_stack src}\n`
|
|
|
|
+ | {loc = Stack s; typ = (Val | Int | Addr)}, {loc = Reg _} ->
|
|
|
|
+ let ofs = slot_offset s (register_class src) in
|
|
|
|
+ emit_load dst ofs
|
|
|
|
| {loc = Stack _; typ = Float}, {loc = Reg _} ->
|
|
|
|
` fld {emit_reg dst}, {emit_stack src}\n`
|
|
|
|
| _ ->
|
2016-11-23 10:47:58 +00:00
|
|
|
@@ -263,8 +294,7 @@ let emit_instr i =
|
2016-11-22 22:21:33 +00:00
|
|
|
let n = frame_size() in
|
|
|
|
if !contains_calls then
|
|
|
|
` {emit_string lg} ra, {emit_int(n - size_addr)}(sp)\n`;
|
|
|
|
- if n > 0 then
|
|
|
|
- ` addi sp, sp, {emit_int n}\n`;
|
|
|
|
+ emit_stack_adjustment n;
|
|
|
|
` jr {emit_reg i.arg.(0)}\n`
|
|
|
|
| Lop(Itailcall_imm {func; label_after = _}) ->
|
|
|
|
if func = !function_name then begin
|
2016-11-23 10:47:58 +00:00
|
|
|
@@ -273,8 +303,7 @@ let emit_instr i =
|
2016-11-22 22:21:33 +00:00
|
|
|
let n = frame_size() in
|
|
|
|
if !contains_calls then
|
|
|
|
` {emit_string lg} ra, {emit_int(n - size_addr)}(sp)\n`;
|
|
|
|
- if n > 0 then
|
|
|
|
- ` addi sp, sp, {emit_int n}\n`;
|
|
|
|
+ emit_stack_adjustment n;
|
|
|
|
` tail {emit_symbol func}\n`
|
|
|
|
end
|
|
|
|
| Lop(Iextcall{func; alloc = true; label_after = label}) ->
|
2016-11-23 10:47:58 +00:00
|
|
|
@@ -285,7 +314,7 @@ let emit_instr i =
|
2016-11-22 22:21:33 +00:00
|
|
|
` call {emit_symbol func}\n`
|
|
|
|
| Lop(Istackoffset n) ->
|
|
|
|
assert (n mod 16 = 0);
|
|
|
|
- ` addi sp, sp, {emit_int (-n)}\n`;
|
|
|
|
+ emit_stack_adjustment (-n);
|
|
|
|
stack_offset := !stack_offset + n
|
|
|
|
| Lop(Iload(Single, Iindexed ofs)) ->
|
|
|
|
` flw {emit_reg i.res.(0)}, {emit_int ofs}({emit_reg i.arg.(0)})\n`;
|
2016-11-23 10:47:58 +00:00
|
|
|
@@ -398,8 +427,7 @@ let emit_instr i =
|
2016-11-22 22:21:33 +00:00
|
|
|
` {emit_string lg} ra, {emit_int(n - size_addr)}(sp)\n`
|
|
|
|
| Lreturn ->
|
|
|
|
let n = frame_size() in
|
|
|
|
- if n > 0 then
|
|
|
|
- ` addi sp, sp, {emit_int n}\n`;
|
|
|
|
+ emit_stack_adjustment n;
|
|
|
|
` ret\n`
|
|
|
|
| Llabel lbl ->
|
|
|
|
`{emit_label lbl}:\n`
|
2016-11-23 10:47:58 +00:00
|
|
|
@@ -513,8 +541,7 @@ let fundecl fundecl =
|
2016-11-22 22:21:33 +00:00
|
|
|
` .align 2\n`;
|
|
|
|
`{emit_symbol fundecl.fun_name}:\n`;
|
|
|
|
let n = frame_size() in
|
|
|
|
- if n > 0 then
|
|
|
|
- ` addi sp, sp, {emit_int(-n)}\n`;
|
|
|
|
+ emit_stack_adjustment (-n);
|
|
|
|
if !contains_calls then
|
|
|
|
` {emit_string stg} ra, {emit_int(n - size_addr)}(sp)\n`;
|
|
|
|
`{emit_label !tailrec_entry_point}:\n`;
|
|
|
|
diff --git a/asmcomp/riscv/selection.ml b/asmcomp/riscv/selection.ml
|
2017-05-10 11:50:40 +00:00
|
|
|
index ad2b26e9b..283233679 100644
|
2016-11-22 22:21:33 +00:00
|
|
|
--- a/asmcomp/riscv/selection.ml
|
|
|
|
+++ b/asmcomp/riscv/selection.ml
|
|
|
|
@@ -22,7 +22,7 @@ class selector = object (self)
|
|
|
|
|
|
|
|
inherit Selectgen.selector_generic as super
|
|
|
|
|
|
|
|
-method is_immediate n = (n <= 0x7FF) && (n >= -0x800)
|
|
|
|
+method is_immediate n = is_immediate n
|
|
|
|
|
|
|
|
method select_addressing _ = function
|
|
|
|
| Cop(Cadda, [arg; Cconst_int n]) when self#is_immediate n ->
|
|
|
|
--
|
2017-06-26 09:53:06 +00:00
|
|
|
2.13.1
|
2016-11-22 22:21:33 +00:00
|
|
|
|