浏览代码

* enable tail recursion optimization also for const and constref open arrays
+ added test

git-svn-id: trunk@44011 -

svenbarth 5 年之前
父节点
当前提交
4fc02d3d09
共有 3 个文件被更改,包括 78 次插入6 次删除
  1. 1 0
      .gitattributes
  2. 8 6
      compiler/opttail.pas
  3. 69 0
      tests/tbs/tb0667.pp

+ 1 - 0
.gitattributes

@@ -13033,6 +13033,7 @@ tests/tbs/tb0664.pp svneol=native#text/pascal
 tests/tbs/tb0665.pp svneol=native#text/pascal
 tests/tbs/tb0665.pp svneol=native#text/pascal
 tests/tbs/tb0666a.pp svneol=native#text/pascal
 tests/tbs/tb0666a.pp svneol=native#text/pascal
 tests/tbs/tb0666b.pp svneol=native#text/pascal
 tests/tbs/tb0666b.pp svneol=native#text/pascal
+tests/tbs/tb0667.pp svneol=native#text/pascal
 tests/tbs/ub0060.pp svneol=native#text/plain
 tests/tbs/ub0060.pp svneol=native#text/plain
 tests/tbs/ub0069.pp svneol=native#text/plain
 tests/tbs/ub0069.pp svneol=native#text/plain
 tests/tbs/ub0119.pp svneol=native#text/plain
 tests/tbs/ub0119.pp svneol=native#text/plain

+ 8 - 6
compiler/opttail.pas

@@ -77,6 +77,7 @@ unit opttail;
           tempnode : ttempcreatenode;
           tempnode : ttempcreatenode;
           loadnode : tloadnode;
           loadnode : tloadnode;
           oldnodetree : tnode;
           oldnodetree : tnode;
+          useaddr : boolean;
         begin
         begin
           { no tail call found and replaced so far }
           { no tail call found and replaced so far }
           result:=false;
           result:=false;
@@ -120,7 +121,10 @@ unit opttail;
                     paranode:=tcallparanode(usedcallnode.left);
                     paranode:=tcallparanode(usedcallnode.left);
                     while assigned(paranode) do
                     while assigned(paranode) do
                       begin
                       begin
-                        if paranode.parasym.varspez=vs_var then
+                        useaddr:=(paranode.parasym.varspez in [vs_var,vs_constref]) or
+                          ((paranode.parasym.varspez=vs_const) and
+                          paramanager.push_addr_param(paranode.parasym.varspez,paranode.parasym.vardef,p.proccalloption));
+                        if useaddr then
                           begin
                           begin
                             tempnode:=ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
                             tempnode:=ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
                             addstatement(calcstatements,tempnode);
                             addstatement(calcstatements,tempnode);
@@ -146,7 +150,7 @@ unit opttail;
                         include(tloadnode(loadnode).loadnodeflags,loadnf_isinternal_ignoreconst);
                         include(tloadnode(loadnode).loadnodeflags,loadnf_isinternal_ignoreconst);
 
 
                         { load the address of the symbol instead of symbol }
                         { load the address of the symbol instead of symbol }
-                        if paranode.parasym.varspez=vs_var then
+                        if useaddr then
                           include(tloadnode(loadnode).loadnodeflags,loadnf_load_addr);
                           include(tloadnode(loadnode).loadnodeflags,loadnf_load_addr);
                         addstatement(copystatements,
                         addstatement(copystatements,
                           cassignmentnode.create_internal(
                           cassignmentnode.create_internal(
@@ -206,14 +210,12 @@ unit opttail;
         { check if the parameters actually would support tail recursion elimination }
         { check if the parameters actually would support tail recursion elimination }
         for i:=0 to p.paras.count-1 do
         for i:=0 to p.paras.count-1 do
           with tparavarsym(p.paras[i]) do
           with tparavarsym(p.paras[i]) do
-            if (varspez in [vs_out,{vs_var,}vs_constref]) or
-              ((varspez=vs_const) and
-               (paramanager.push_addr_param(varspez,vardef,p.proccalloption)) or
+            if (varspez=vs_out) or
                { parameters requiring tables are too complicated to handle
                { parameters requiring tables are too complicated to handle
                  and slow down things anyways so a tail recursion call
                  and slow down things anyways so a tail recursion call
                  makes no sense
                  makes no sense
                }
                }
-               is_managed_type(vardef)) then
+               is_managed_type(vardef) then
                exit;
                exit;
 
 
         labelsym:=clabelsym.create('$opttail');
         labelsym:=clabelsym.create('$opttail');

+ 69 - 0
tests/tbs/tb0667.pp

@@ -0,0 +1,69 @@
+{ %OPT=-Ootailrec }
+
+program tb0667;
+
+var
+  stack: Pointer = Nil;
+
+function Test1(var Values: array of LongInt; Res: LongInt): LongInt;
+begin
+  if not Assigned(stack) then
+    stack := get_frame
+  else if stack <> get_frame then
+    Halt(1);
+  if Length(Values) = 0 then
+    Test1 := Res
+  else
+    Test1 := Test1(Values[0..High(Values) - 1], Res + Values[High(Values)]);
+end;
+
+function Test1(Values: array of LongInt): LongInt;
+begin
+  Test1 := Test1(Values, 0);
+end;
+
+function Test2(constref Values: array of LongInt; Res: LongInt): LongInt;
+begin
+  if not Assigned(stack) then
+    stack := get_frame
+  else if stack <> get_frame then
+    Halt(3);
+  if Length(Values) = 0 then
+    Test2 := Res
+  else
+    Test2 := Test2(Values[0..High(Values) - 1], Res + Values[High(Values)]);
+end;
+
+function Test2(constref Values: array of LongInt): LongInt;
+begin
+  Test2 := Test2(Values, 0);
+end;
+
+function Test3(const Values: array of LongInt; Res: LongInt): LongInt;
+begin
+  if not Assigned(stack) then
+    stack := get_frame
+  else if stack <> get_frame then
+    Halt(5);
+  if Length(Values) = 0 then
+    Test3 := Res
+  else
+    Test3 := Test3(Values[0..High(Values) - 1], Res + Values[High(Values)]);
+end;
+
+function Test3(const Values: array of LongInt): LongInt;
+begin
+  Test3 := Test3(Values, 0);
+end;
+
+begin
+  if Test1([1, 2, 3, 4]) <> 10 then
+    Halt(2);
+  stack := Nil;
+  if Test2([1, 2, 3, 4]) <> 10 then
+    Halt(4);
+  stack := Nil;
+  if Test3([1, 2, 3, 4]) <> 10 then
+    Halt(6);
+  writeln('ok');
+end.