@@ -83,6 +83,8 @@ proc isInCurrentFrame(p: BProc, n: PNode): bool =
8383 result = isInCurrentFrame (p, n.sons[0 ])
8484 else : discard
8585
86+ proc genIndexCheck (p: BProc ; arr, idx: TLoc )
87+
8688proc openArrayLoc (p: BProc , n: PNode ): Rope =
8789 var a: TLoc
8890
@@ -93,18 +95,28 @@ proc openArrayLoc(p: BProc, n: PNode): Rope =
9395 initLocExpr (p, q[1 ], a)
9496 initLocExpr (p, q[2 ], b)
9597 initLocExpr (p, q[3 ], c)
96- let fmt =
97- case skipTypes (a.t, abstractVar+ {tyPtr}).kind
98- of tyOpenArray, tyVarargs, tyArray:
99- " ($1)+($2), ($3)-($2)+1"
100- of tyString, tySequence:
101- if skipTypes (n.typ, abstractInst).kind == tyVar and
102- not compileToCpp (p.module):
103- " (*$1)->data+($2), ($3)-($2)+1"
104- else :
105- " $1->data+($2), ($3)-($2)+1"
106- else : (internalError (" openArrayLoc: " & typeToString (a.t)); " " )
107- result = fmt % [rdLoc (a), rdLoc (b), rdLoc (c)]
98+ # but first produce the required index checks:
99+ if optBoundsCheck in p.options:
100+ genIndexCheck (p, a, b)
101+ genIndexCheck (p, a, c)
102+ let ty = skipTypes (a.t, abstractVar+ {tyPtr})
103+ case ty.kind
104+ of tyArray:
105+ let first = firstOrd (ty)
106+ if first == 0 :
107+ result = " ($1)+($2), ($3)-($2)+1" % [rdLoc (a), rdLoc (b), rdLoc (c)]
108+ else :
109+ result = " ($1)+(($2)-($4)), ($3)-($2)+1" % [rdLoc (a), rdLoc (b), rdLoc (c), intLiteral (first)]
110+ of tyOpenArray, tyVarargs:
111+ result = " ($1)+($2), ($3)-($2)+1" % [rdLoc (a), rdLoc (b), rdLoc (c)]
112+ of tyString, tySequence:
113+ if skipTypes (n.typ, abstractInst).kind == tyVar and
114+ not compileToCpp (p.module):
115+ result = " (*$1)->data+($2), ($3)-($2)+1" % [rdLoc (a), rdLoc (b), rdLoc (c)]
116+ else :
117+ result = " $1->data+($2), ($3)-($2)+1" % [rdLoc (a), rdLoc (b), rdLoc (c)]
118+ else :
119+ internalError (" openArrayLoc: " & typeToString (a.t))
108120 else :
109121 initLocExpr (p, n, a)
110122 case skipTypes (a.t, abstractVar).kind
0 commit comments