Browse Source

* transform -1-x into not(x)

florian 1 year ago
parent
commit
594d4cc4f9
2 changed files with 13 additions and 2 deletions
  1. 8 1
      compiler/nadd.pas
  2. 5 1
      tests/tbs/tb0711.pp

+ 8 - 1
compiler/nadd.pas

@@ -916,8 +916,15 @@ implementation
         { Deal with anti-commutative subtraction }
         if (nodetype = subn) then
           begin
+            { transform -1-x into not(x) }
+            if is_signed(rd) and is_constintnode(left) and (tordconstnode(left).value=-1)  then
+              begin
+                result:=cnotnode.create(right.getcopy);
+                exit;
+              end
+
             { change "0 - val" to "-val" }
-            if is_constintnode(left) and (is_integer(right.resultdef) or is_pointer(right.resultdef)) then
+            else if is_constintnode(left) and (is_integer(right.resultdef) or is_pointer(right.resultdef)) then
               begin
                 if (tordconstnode(left).value = 0) then
                   result := ctypeconvnode.create_internal(cunaryminusnode.create(right.getcopy),right.resultdef);

+ 5 - 1
tests/tbs/tb0711.pp

@@ -1,3 +1,4 @@
+{ %opt=-O- -O1 }
 var
   l : longint;
 
@@ -11,5 +12,8 @@ begin
     halt(2);
 
   if -(1+l)<>-1235 then
-    halt(2);
+    halt(3);
+
+  if -1-l<>-1235 then
+    halt(4);
 end.