Browse Source

* sizeof(array) and length(array) must return sizeUint in case the array
size does not fit in sizeint. This fixes a range check error during
compilation on small CPU targets where allowed array size is 64K, but
sizeint is 32K max.

Yuriy Sydorov 4 years ago
parent
commit
07dcd22b1c
3 changed files with 19 additions and 6 deletions
  1. 15 1
      compiler/ncon.pas
  2. 2 3
      compiler/ninl.pas
  3. 2 2
      compiler/pexpr.pas

+ 15 - 1
compiler/ncon.pas

@@ -202,7 +202,10 @@ interface
        cguidconstnode : tguidconstnodeclass = tguidconstnode;
        cnilnode : tnilnodeclass=tnilnode;
 
-    function genintconstnode(const v : TConstExprInt) : tordconstnode;
+    { Creates tordconstnode with the smallest possible int type which can hold v }
+    function genintconstnode(const v : TConstExprInt) : tordconstnode; overload;
+    { Creates tordconstnode with the preferredinttype type or a bigger type which can hold v }
+    function genintconstnode(const v : TConstExprInt; preferredinttype : tdef) : tordconstnode; overload;
     function genenumnode(v : tenumsym) : tordconstnode;
 
     { some helper routines }
@@ -233,6 +236,17 @@ implementation
       end;
 
 
+    function genintconstnode(const v : TConstExprInt; preferredinttype : tdef) : tordconstnode;
+      var
+        htype : tdef;
+      begin
+        int_to_type(v,htype);
+        if htype.size<preferredinttype.size then
+          htype:=preferredinttype;
+        result:=cordconstnode.create(v,htype,true);
+      end;
+
+
     function genenumnode(v : tenumsym) : tordconstnode;
       var
         htype : tdef;

+ 2 - 3
compiler/ninl.pas

@@ -2527,9 +2527,8 @@ implementation
                         else if not is_open_array(left.resultdef) and
                            not is_array_of_const(left.resultdef) and
                            not is_dynamic_array(left.resultdef) then
-                          result:=cordconstnode.create(tarraydef(left.resultdef).highrange-
-                            tarraydef(left.resultdef).lowrange+1,
-                            sinttype,true);
+                          result:=genintconstnode(tarraydef(left.resultdef).highrange-
+                            tarraydef(left.resultdef).lowrange+1,sizesinttype);
                       end;
                     else
                       ;

+ 2 - 2
compiler/pexpr.pas

@@ -465,12 +465,12 @@ implementation
                      not((p1.nodetype = subscriptn) and
                          is_packed_record_or_object(tsubscriptnode(p1).left.resultdef))) then
                    begin
-                     statement_syssym:=cordconstnode.create(p1.resultdef.size,sizesinttype,true);
+                     statement_syssym:=genintconstnode(p1.resultdef.size,sizesinttype);
                      if (l = in_bitsizeof_x) then
                        statement_syssym:=caddnode.create(muln,statement_syssym,cordconstnode.create(8,sizesinttype,true));
                    end
                  else
-                   statement_syssym:=cordconstnode.create(p1.resultdef.packedbitsize,sizesinttype,true);
+                   statement_syssym:=genintconstnode(p1.resultdef.packedbitsize,sizesinttype);
                  { type def is a struct with generic fields }
                  if df_has_generic_fields in p1.resultdef.defoptions then
                     include(statement_syssym.flags,nf_generic_para);