Step 5: parser and code generator gen.pas turns items (constants, variables, values in rax or on the stack, procedures) into code: a stack machine through rax, calls in the System V convention with arguments computed into frame slots first, aggregates by the address of a copy, frames zeroed on entry, checked indexing and slicing that stop in Rt.fail. parse.pas reads modules in one pass, compiling each import where it stands; module bodies, per-target files with the interface check (a fingerprint of each export), code procedures, variants and match, forward procedures, break. jed.pas writes the entry point, which runs the bodies of the modules used and then main. Everything in the report but multiple results, errors, enumerations and sets, record literals, defer, NEW, generic modules and extern.
| @@ -0,0 +1,1177 @@ | ||
| 1 | +{ gen.pas: the code generator. | |
| 2 | + | |
| 3 | + The parser describes each operand as an item (TItem), as in Wirth's | |
| 4 | + compilers: a constant, a variable at an address the code can name, a | |
| 5 | + value in rax, a value pushed on the stack, a procedure. The routines | |
| 6 | + here turn items into code. They are a stack machine through rax: a | |
| 7 | + value is computed into rax, and pushed while another is computed, so | |
| 8 | + no register is ever allocated. Every access the program writes is one | |
| 9 | + instruction, in the order written. | |
| 10 | + | |
| 11 | + Calls use the System V convention. Arguments are computed left to | |
| 12 | + right into frame slots, then loaded into registers just before the | |
| 13 | + call, so that computing one argument can't disturb another. A record, | |
| 14 | + array, slice or variant passed by value goes as the address of a copy | |
| 15 | + the caller makes; one returned goes through a hidden pointer, as C's | |
| 16 | + structs do. | |
| 17 | + | |
| 18 | + A procedure's frame holds its parameters, its variables, and the | |
| 19 | + temporaries of the statement running. All of it is zeroed on entry. | |
| 20 | + | |
| 21 | + Depends on: scan for errors, sym for types, x64 for instructions. | |
| 22 | + Leaves out: the language's syntax; that is parse.pas. } | |
| 23 | + | |
| 24 | +unit gen; | |
| 25 | + | |
| 26 | +{$mode objfpc}{$H-} | |
| 27 | + | |
| 28 | +interface | |
| 29 | + | |
| 30 | +uses scan, sym, x64; | |
| 31 | + | |
| 32 | +type | |
| 33 | + TMode = (mConst, mVar, mReg, mStack, mProc, mType, mStr, mBuiltin, mCase, mNone); | |
| 34 | + TBase = (bFrame, bGlobal, bReg, bStack); | |
| 35 | + | |
| 36 | + TItem = record | |
| 37 | + mode: TMode; | |
| 38 | + typ: PType; | |
| 39 | + val: int64; { mConst: the value, an F64's bits; mStr: its length } | |
| 40 | + base: TBase; { mVar: what the address is relative to } | |
| 41 | + reg: longint; { mVar with bReg: the register } | |
| 42 | + off: int64; { mVar: added to the base } | |
| 43 | + chunk: longint; { mVar with bGlobal, mStr: the chunk } | |
| 44 | + vol: boolean; { mVar: reached through ^VOLATILE } | |
| 45 | + isVar: boolean; { mVar: a variable of the program, not a temporary } | |
| 46 | + obj: PObj; { mProc, mBuiltin, mCase } | |
| 47 | + end; | |
| 48 | + | |
| 49 | + TCall = record | |
| 50 | + sig: PType; | |
| 51 | + param: PObj; { the parameter the next argument is for } | |
| 52 | + slots: array[0..63] of int64; | |
| 53 | + n: longint; | |
| 54 | + target: int64; { the frame slot of a procedure value, if any } | |
| 55 | + chunk: longint; { else the procedure's chunk } | |
| 56 | + end; | |
| 57 | + | |
| 58 | +var | |
| 59 | + rtFail, rtEq, argcChunk, argvChunk: longint; | |
| 60 | + fileChunk: longint; { the current file's name, for failed checks; -1 until needed } | |
| 61 | + depth: longint; { values pushed and not yet popped } | |
| 62 | + | |
| 63 | +procedure MakeConst(var x: TItem; t: PType; v: int64); | |
| 64 | +procedure MakeReal(var x: TItem; r: double); | |
| 65 | +procedure MakeStr(var x: TItem); | |
| 66 | +procedure MakeItem(var x: TItem; o: PObj); | |
| 67 | +function RealOf(const x: TItem): double; | |
| 68 | + | |
| 69 | +procedure Value(var x: TItem); | |
| 70 | +procedure Address(var x: TItem; r: longint); | |
| 71 | +procedure Save(var x: TItem); | |
| 72 | +procedure Coerce(var x: TItem; t: PType); | |
| 73 | +procedure Assign(var dst, src: TItem); | |
| 74 | +procedure CopyToTemp(var x: TItem); | |
| 75 | +function ToSlot(var x: TItem): int64; | |
| 76 | + | |
| 77 | +procedure Deref(var x: TItem); | |
| 78 | +procedure Field(var x: TItem; f: PObj); | |
| 79 | +procedure IndexPrep(var x: TItem); | |
| 80 | +procedure Index(var x, y: TItem); | |
| 81 | +procedure SliceLo(var lo: TItem); | |
| 82 | +procedure Slice(var x, lo, hi: TItem); | |
| 83 | + | |
| 84 | +procedure Neg(var x: TItem); | |
| 85 | +procedure NotOp(var x: TItem); | |
| 86 | +procedure Op1(op: TSym; var x: TItem; var L: longint); | |
| 87 | +procedure Op2(op: TSym; var x, y: TItem; L: longint); | |
| 88 | +procedure Convert(var x: TItem; t: PType); | |
| 89 | + | |
| 90 | +procedure BeginCall(var c: TCall; var x: TItem); | |
| 91 | +procedure PassArg(var c: TCall; var a: TItem); | |
| 92 | +procedure EndCall(var c: TCall; var x: TItem); | |
| 93 | + | |
| 94 | +procedure Construct(var x: TItem; c: PObj); | |
| 95 | +procedure SetField(const x: TItem; f: PObj; var a: TItem); | |
| 96 | +function MatchBegin(var x: TItem): int64; | |
| 97 | +procedure MatchTag(p: int64; tag: int64; var L: longint); | |
| 98 | +procedure CondJump(var x: TItem; var L: longint); | |
| 99 | + | |
| 100 | +procedure SliceOf(var x: TItem; t: PType; len: int64); | |
| 101 | +procedure TempItem(var x: TItem; t: PType; off: int64); | |
| 102 | +function ScalarSize(t: PType): longint; | |
| 103 | +procedure Norm(t: PType); | |
| 104 | +procedure ForNext(o: PObj; bslot, step: int64; var exitL: longint; top: longint); | |
| 105 | +function AllocTemp(size, align: int64): int64; | |
| 106 | +function AllocPerm(size, align: int64): int64; | |
| 107 | +function TempMark: int64; | |
| 108 | +procedure ReleaseTemps(m: int64); | |
| 109 | + | |
| 110 | +procedure BeginProc(sig: PType; params: PObj); | |
| 111 | +procedure EnterBody(c: longint); | |
| 112 | +procedure ReturnValue(var x: TItem); | |
| 113 | +procedure ReturnNone; | |
| 114 | +procedure EndProc(c: longint); | |
| 115 | +procedure CmpI(r: longint; v: int64); | |
| 116 | +procedure PushRax; | |
| 117 | +procedure PopR(r: longint); | |
| 118 | + | |
| 119 | +implementation | |
| 120 | + | |
| 121 | +var | |
| 122 | + frameTop, frameMax, permTop: int64; | |
| 123 | + frameFix, zeroFix: longint; { where the frame size is patched in } | |
| 124 | + retSlot: int64; { holds the hidden result pointer } | |
| 125 | + result: PType; { the procedure's result, or nil } | |
| 126 | + exitL: longint; | |
| 127 | + { parameters arriving in registers: where each is stored on entry } | |
| 128 | + inReg: array[0..13] of longint; | |
| 129 | + inSlot: array[0..13] of int64; | |
| 130 | + inSize: array[0..13] of longint; | |
| 131 | + numIn: longint; | |
| 132 | + | |
| 133 | +const | |
| 134 | + IntRegs: array[0..5] of longint = (RDI, RSI, RDX, RCX, R8, R9); | |
| 135 | + | |
| 136 | +{ Items } | |
| 137 | + | |
| 138 | +procedure MakeConst(var x: TItem; t: PType; v: int64); | |
| 139 | +begin | |
| 140 | + FillChar(x, SizeOf(x), 0); | |
| 141 | + x.mode := mConst; | |
| 142 | + x.typ := t; | |
| 143 | + x.val := v; | |
| 144 | +end; | |
| 145 | + | |
| 146 | +procedure MakeReal(var x: TItem; r: double); | |
| 147 | +begin | |
| 148 | + MakeConst(x, tReal, 0); | |
| 149 | + Move(r, x.val, 8); | |
| 150 | +end; | |
| 151 | + | |
| 152 | +function RealOf(const x: TItem): double; | |
| 153 | +var r: double; | |
| 154 | +begin | |
| 155 | + Move(x.val, r, 8); | |
| 156 | + RealOf := r; | |
| 157 | +end; | |
| 158 | + | |
| 159 | +{ The current string literal, as an item of its own chunk. } | |
| 160 | +procedure MakeStr(var x: TItem); | |
| 161 | +begin | |
| 162 | + FillChar(x, SizeOf(x), 0); | |
| 163 | + x.mode := mStr; | |
| 164 | + x.typ := tStr; | |
| 165 | + x.val := slen; | |
| 166 | + x.chunk := NewChunk(secRodata, -1, -1); | |
| 167 | + SetData(x.chunk, @sbuf, slen + 1, 1); | |
| 168 | +end; | |
| 169 | + | |
| 170 | +procedure MakeItem(var x: TItem; o: PObj); | |
| 171 | +begin | |
| 172 | + FillChar(x, SizeOf(x), 0); | |
| 173 | + x.typ := o^.typ; | |
| 174 | + x.obj := o; | |
| 175 | + case o^.cls of | |
| 176 | + oConst: | |
| 177 | + if o^.typ = tStr then begin | |
| 178 | + x.mode := mStr; x.val := o^.val; x.chunk := o^.chunk | |
| 179 | + end else begin | |
| 180 | + x.mode := mConst; x.val := o^.val | |
| 181 | + end; | |
| 182 | + oVar: begin | |
| 183 | + x.mode := mVar; | |
| 184 | + x.isVar := true; | |
| 185 | + if o^.lev = 0 then begin | |
| 186 | + x.base := bGlobal; x.chunk := o^.chunk | |
| 187 | + end else if o^.ind then begin | |
| 188 | + Load(RAX, Mem(RBP, o^.val), 8, false); | |
| 189 | + x.base := bReg; x.reg := RAX; x.off := o^.off; | |
| 190 | + end else begin | |
| 191 | + x.base := bFrame; x.off := o^.val | |
| 192 | + end; | |
| 193 | + end; | |
| 194 | + oProc: x.mode := mProc; | |
| 195 | + oType: x.mode := mType; | |
| 196 | + oBuiltin: x.mode := mBuiltin; | |
| 197 | + oCase: x.mode := mCase; | |
| 198 | + else Mark('not a value'); | |
| 199 | + end; | |
| 200 | +end; | |
| 201 | + | |
| 202 | +procedure PushRax; | |
| 203 | +begin | |
| 204 | + Push(RAX); | |
| 205 | + inc(depth); | |
| 206 | +end; | |
| 207 | + | |
| 208 | +procedure PopR(r: longint); | |
| 209 | +begin | |
| 210 | + Pop(r); | |
| 211 | + dec(depth); | |
| 212 | +end; | |
| 213 | + | |
| 214 | +procedure CmpI(r: longint; v: int64); | |
| 215 | +begin | |
| 216 | + if (v >= low(longint)) and (v <= high(longint)) then AluI(aluCmp, r, v) | |
| 217 | + else begin MovRI(R11, v); Alu(aluCmp, r, R11) end; | |
| 218 | +end; | |
| 219 | + | |
| 220 | +function MemOf(var x: TItem; r: longint): TMem; | |
| 221 | +begin | |
| 222 | + case x.base of | |
| 223 | + bFrame: MemOf := Mem(RBP, x.off); | |
| 224 | + bGlobal: MemOf := MemRip(x.chunk, x.off); | |
| 225 | + bStack: begin | |
| 226 | + PopR(r); | |
| 227 | + x.base := bReg; x.reg := r; | |
| 228 | + MemOf := Mem(r, x.off); | |
| 229 | + end; | |
| 230 | + else MemOf := Mem(x.reg, x.off); | |
| 231 | + end; | |
| 232 | +end; | |
| 233 | + | |
| 234 | +function ScalarSize(t: PType): longint; | |
| 235 | +begin | |
| 236 | + if t^.form in [fInt, fBool, fEnum, fSet] then ScalarSize := t^.size | |
| 237 | + else ScalarSize := 8; | |
| 238 | +end; | |
| 239 | + | |
| 240 | +{ Brings a value just computed in rax to its type's width. } | |
| 241 | +procedure Norm(t: PType); | |
| 242 | +begin | |
| 243 | + if (t^.form = fInt) and (t^.size < 8) then Extend(RAX, t^.size, t^.signed); | |
| 244 | +end; | |
| 245 | + | |
| 246 | +function Cut(v: int64; t: PType): int64; | |
| 247 | +begin | |
| 248 | + Cut := v; | |
| 249 | + if t^.form <> fInt then exit; | |
| 250 | + case t^.size of | |
| 251 | + 1: if t^.signed then Cut := shortint(v) else Cut := byte(v); | |
| 252 | + 2: if t^.signed then Cut := smallint(v) else Cut := word(v); | |
| 253 | + 4: if t^.signed then Cut := longint(v) else Cut := longword(v); | |
| 254 | + end; | |
| 255 | +end; | |
| 256 | + | |
| 257 | +procedure Value(var x: TItem); | |
| 258 | +var m: TMem; | |
| 259 | +begin | |
| 260 | + case x.mode of | |
| 261 | + mConst: MovRI(RAX, x.val); | |
| 262 | + mVar: begin | |
| 263 | + if not IsScalar(x.typ) then Mark('not a single value'); | |
| 264 | + m := MemOf(x, RAX); | |
| 265 | + Load(RAX, m, ScalarSize(x.typ), (x.typ^.form = fInt) and x.typ^.signed); | |
| 266 | + end; | |
| 267 | + mReg: ; | |
| 268 | + mStack: PopR(RAX); | |
| 269 | + mProc: Lea(RAX, MemRip(x.obj^.chunk, 0)); | |
| 270 | + else Mark('not a value'); | |
| 271 | + end; | |
| 272 | + x.mode := mReg; | |
| 273 | +end; | |
| 274 | + | |
| 275 | +procedure Address(var x: TItem; r: longint); | |
| 276 | +var m: TMem; | |
| 277 | +begin | |
| 278 | + if x.mode = mStr then Lea(r, MemRip(x.chunk, 0)) | |
| 279 | + else if x.mode = mVar then begin | |
| 280 | + m := MemOf(x, r); | |
| 281 | + if (x.base <> bReg) or (x.reg <> r) or (x.off <> 0) then Lea(r, m); | |
| 282 | + x.base := bReg; x.reg := r; x.off := 0; | |
| 283 | + end else Mark('not a variable'); | |
| 284 | +end; | |
| 285 | + | |
| 286 | +procedure Save(var x: TItem); | |
| 287 | +begin | |
| 288 | + if x.mode = mReg then begin | |
| 289 | + PushRax; x.mode := mStack | |
| 290 | + end else if (x.mode = mVar) and (x.base = bReg) then begin | |
| 291 | + if x.reg <> RAX then MovRR(RAX, x.reg); | |
| 292 | + PushRax; x.base := bStack; | |
| 293 | + end; | |
| 294 | +end; | |
| 295 | + | |
| 296 | +{ Frames } | |
| 297 | + | |
| 298 | +function AllocTemp(size, align: int64): int64; | |
| 299 | +begin | |
| 300 | + if align < 1 then align := 1; | |
| 301 | + frameTop := AlignUp(frameTop + size, align); | |
| 302 | + if frameTop > frameMax then frameMax := frameTop; | |
| 303 | + AllocTemp := -frameTop; | |
| 304 | +end; | |
| 305 | + | |
| 306 | +function AllocPerm(size, align: int64): int64; | |
| 307 | +begin | |
| 308 | + AllocPerm := AllocTemp(size, align); | |
| 309 | + permTop := frameTop; | |
| 310 | +end; | |
| 311 | + | |
| 312 | +function TempMark: int64; | |
| 313 | +begin | |
| 314 | + TempMark := frameTop; | |
| 315 | +end; | |
| 316 | + | |
| 317 | +procedure ReleaseTemps(m: int64); | |
| 318 | +begin | |
| 319 | + if m < permTop then m := permTop; | |
| 320 | + frameTop := m; | |
| 321 | +end; | |
| 322 | + | |
| 323 | +procedure TempItem(var x: TItem; t: PType; off: int64); | |
| 324 | +begin | |
| 325 | + FillChar(x, SizeOf(x), 0); | |
| 326 | + x.mode := mVar; x.typ := t; x.base := bFrame; x.off := off; | |
| 327 | +end; | |
| 328 | + | |
| 329 | +function ToSlot(var x: TItem): int64; | |
| 330 | +var s: int64; | |
| 331 | +begin | |
| 332 | + Value(x); | |
| 333 | + s := AllocTemp(8, 8); | |
| 334 | + Store(Mem(RBP, s), RAX, 8); | |
| 335 | + ToSlot := s; | |
| 336 | +end; | |
| 337 | + | |
| 338 | +{ Copying } | |
| 339 | + | |
| 340 | +procedure Copy(size: int64); | |
| 341 | +begin | |
| 342 | + MovRI(RCX, size); | |
| 343 | + RepMovsb; | |
| 344 | +end; | |
| 345 | + | |
| 346 | +procedure CopyToTemp(var x: TItem); | |
| 347 | +var t: int64; | |
| 348 | +begin | |
| 349 | + if x.vol then Mark('can''t copy through a volatile pointer'); | |
| 350 | + t := AllocTemp(x.typ^.size, x.typ^.align); | |
| 351 | + Address(x, RSI); | |
| 352 | + Lea(RDI, Mem(RBP, t)); | |
| 353 | + Copy(x.typ^.size); | |
| 354 | + TempItem(x, x.typ, t); | |
| 355 | +end; | |
| 356 | + | |
| 357 | +{ A slice made from an address in rax and a length. } | |
| 358 | +procedure SliceOf(var x: TItem; t: PType; len: int64); | |
| 359 | +var s: int64; | |
| 360 | +begin | |
| 361 | + s := AllocTemp(16, 8); | |
| 362 | + Store(Mem(RBP, s), RAX, 8); | |
| 363 | + MovRI(RAX, len); | |
| 364 | + Store(Mem(RBP, s + 8), RAX, 8); | |
| 365 | + TempItem(x, t, s); | |
| 366 | +end; | |
| 367 | + | |
| 368 | +procedure Coerce(var x: TItem; t: PType); | |
| 369 | +var s: PType; tmp: int64; | |
| 370 | +begin | |
| 371 | + s := x.typ; | |
| 372 | + if x.mode = mStr then begin | |
| 373 | + if (t^.form = fSlice) and (t^.base = tU8) then begin | |
| 374 | + Address(x, RAX); SliceOf(x, t, x.val) | |
| 375 | + end else if (t^.form = fPtr) and (t^.base = tU8) then begin | |
| 376 | + Address(x, RAX); x.mode := mReg; x.typ := t | |
| 377 | + end else if (t^.form = fArray) and (t^.base = tU8) and (t^.len >= x.val) then begin | |
| 378 | + tmp := AllocTemp(t^.size, 1); | |
| 379 | + Lea(RDI, Mem(RBP, tmp)); | |
| 380 | + Lea(RSI, MemRip(x.chunk, 0)); | |
| 381 | + Copy(x.val); | |
| 382 | + MovRI(RCX, t^.len - x.val); | |
| 383 | + MovRI(RAX, 0); | |
| 384 | + RepStosb; | |
| 385 | + TempItem(x, t, tmp); | |
| 386 | + end else Mark('can''t use a string as ' + TypeName(t)); | |
| 387 | + exit; | |
| 388 | + end; | |
| 389 | + if x.mode in [mType, mBuiltin, mNone, mCase] then Mark('not a value'); | |
| 390 | + if Same(s, t) then exit; | |
| 391 | + if (s^.form = fConst) and (t^.form = fInt) then begin | |
| 392 | + if (x.mode = mConst) and not Fits(x.val, t) then Mark('constant doesn''t fit ' + TypeName(t)); | |
| 393 | + x.typ := t; | |
| 394 | + end else if (s^.form = fNil) and (t^.form in [fPtr, fProc]) then | |
| 395 | + x.typ := t | |
| 396 | + else if (s^.form = fPtr) and (t^.form = fPtr) and t^.vol and not s^.vol | |
| 397 | + and Same(s^.base, t^.base) then | |
| 398 | + x.typ := t | |
| 399 | + else if (s^.form = fArray) and (t^.form = fSlice) and Same(s^.base, t^.base) then begin | |
| 400 | + if x.vol then Mark('can''t make a slice through a volatile pointer'); | |
| 401 | + Address(x, RAX); | |
| 402 | + SliceOf(x, t, s^.len); | |
| 403 | + end else | |
| 404 | + Mark('can''t use ' + TypeName(s) + ' as ' + TypeName(t)); | |
| 405 | +end; | |
| 406 | + | |
| 407 | +procedure Assign(var dst, src: TItem); | |
| 408 | +var m: TMem; | |
| 409 | +begin | |
| 410 | + Coerce(src, dst.typ); | |
| 411 | + if IsScalar(dst.typ) then begin | |
| 412 | + Value(src); | |
| 413 | + m := MemOf(dst, RCX); | |
| 414 | + Store(m, RAX, ScalarSize(dst.typ)); | |
| 415 | + end else begin | |
| 416 | + if dst.vol or src.vol then Mark('can''t copy through a volatile pointer'); | |
| 417 | + Address(src, RSI); | |
| 418 | + Address(dst, RDI); | |
| 419 | + Copy(dst.typ^.size); | |
| 420 | + end; | |
| 421 | +end; | |
| 422 | + | |
| 423 | +{ Checks } | |
| 424 | + | |
| 425 | +{ The program goes on if the condition cc holds, and otherwise stops | |
| 426 | + in Rt.fail with the file and line. } | |
| 427 | +procedure Check(cc: longint); | |
| 428 | +var L: longint; s: shortstring; | |
| 429 | +begin | |
| 430 | + if fileChunk < 0 then begin | |
| 431 | + s := CurPath + #0; | |
| 432 | + fileChunk := NewChunk(secRodata, -1, -1); | |
| 433 | + SetData(fileChunk, @s[1], length(s), 1); | |
| 434 | + end; | |
| 435 | + L := NoLabel; | |
| 436 | + Jcc(cc, L); | |
| 437 | + AluI(aluAnd, RSP, -16); | |
| 438 | + Lea(RDI, MemRip(fileChunk, 0)); | |
| 439 | + MovRI(RSI, CurLine); | |
| 440 | + CallChunk(rtFail); | |
| 441 | + Ud2; | |
| 442 | + FixLink(L); | |
| 443 | +end; | |
| 444 | + | |
| 445 | +{ Selectors } | |
| 446 | + | |
| 447 | +procedure Deref(var x: TItem); | |
| 448 | +var t: PType; | |
| 449 | +begin | |
| 450 | + t := x.typ; | |
| 451 | + if t^.form <> fPtr then Mark('not a pointer'); | |
| 452 | + if t^.base = nil then Mark('pointer to an undeclared type'); | |
| 453 | + Value(x); | |
| 454 | + x.mode := mVar; x.base := bReg; x.reg := RAX; x.off := 0; | |
| 455 | + x.typ := t^.base; x.vol := t^.vol; x.isVar := true; | |
| 456 | +end; | |
| 457 | + | |
| 458 | +procedure Field(var x: TItem; f: PObj); | |
| 459 | +begin | |
| 460 | + x.off := x.off + f^.val; | |
| 461 | + x.typ := f^.typ; | |
| 462 | +end; | |
| 463 | + | |
| 464 | +procedure IndexPrep(var x: TItem); | |
| 465 | +begin | |
| 466 | + if x.typ^.form = fPtr then begin | |
| 467 | + Value(x); PushRax; x.mode := mStack | |
| 468 | + end else if (x.mode = mVar) and (x.typ^.form in [fArray, fSlice]) then | |
| 469 | + Save(x) | |
| 470 | + else Mark('not an array, slice or pointer'); | |
| 471 | +end; | |
| 472 | + | |
| 473 | +procedure Index(var x, y: TItem); | |
| 474 | +var t: PType; s: int64; m, ml: TMem; | |
| 475 | +begin | |
| 476 | + t := x.typ; | |
| 477 | + s := t^.base^.size; | |
| 478 | + if not IsInt(y.typ) then Mark('an index is an integer'); | |
| 479 | + if t^.form = fPtr then begin | |
| 480 | + Value(y); | |
| 481 | + if s <> 1 then ImulI(RAX, s); | |
| 482 | + PopR(RCX); | |
| 483 | + Alu(aluAdd, RAX, RCX); | |
| 484 | + x.mode := mVar; x.base := bReg; x.reg := RAX; x.off := 0; | |
| 485 | + x.vol := t^.vol; x.isVar := true; | |
| 486 | + end else if (t^.form = fArray) and (y.mode = mConst) then begin | |
| 487 | + if (y.val < 0) or (y.val >= t^.len) then Mark('index out of range'); | |
| 488 | + x.off := x.off + y.val * s; | |
| 489 | + if x.base = bStack then begin | |
| 490 | + PopR(RAX); x.base := bReg; x.reg := RAX | |
| 491 | + end; | |
| 492 | + end else if t^.form = fArray then begin | |
| 493 | + Value(y); | |
| 494 | + CmpI(RAX, t^.len); | |
| 495 | + Check(ccB); | |
| 496 | + if s <> 1 then ImulI(RAX, s); | |
| 497 | + if x.base = bStack then PopR(RCX) else Lea(RCX, MemOf(x, RCX)); | |
| 498 | + Alu(aluAdd, RAX, RCX); | |
| 499 | + if x.base <> bStack then x.off := 0; | |
| 500 | + x.base := bReg; x.reg := RAX; | |
| 501 | + end else begin | |
| 502 | + Value(y); | |
| 503 | + m := MemOf(x, RCX); | |
| 504 | + ml := m; ml.disp := ml.disp + 8; | |
| 505 | + AluM(aluCmp, RAX, ml); | |
| 506 | + Check(ccB); | |
| 507 | + if s <> 1 then ImulI(RAX, s); | |
| 508 | + AluM(aluAdd, RAX, m); | |
| 509 | + x.base := bReg; x.reg := RAX; x.off := 0; x.isVar := true; x.vol := false; | |
| 510 | + end; | |
| 511 | + x.typ := t^.base; | |
| 512 | +end; | |
| 513 | + | |
| 514 | +procedure SliceLo(var lo: TItem); | |
| 515 | +begin | |
| 516 | + if not IsInt(lo.typ) then Mark('an index is an integer'); | |
| 517 | + Value(lo); | |
| 518 | + PushRax; | |
| 519 | +end; | |
| 520 | + | |
| 521 | +procedure Slice(var x, lo, hi: TItem); | |
| 522 | +var t, st: PType; s, tmp: int64; m, ml: TMem; | |
| 523 | +begin | |
| 524 | + t := x.typ; | |
| 525 | + s := t^.base^.size; | |
| 526 | + if not IsInt(hi.typ) then Mark('an index is an integer'); | |
| 527 | + if x.vol then Mark('can''t make a slice through a volatile pointer'); | |
| 528 | + Value(hi); | |
| 529 | + MovRR(RDX, RAX); | |
| 530 | + PopR(RCX); | |
| 531 | + if t^.form = fPtr then PopR(R8) | |
| 532 | + else begin | |
| 533 | + m := MemOf(x, R8); | |
| 534 | + if t^.form = fArray then CmpI(RDX, t^.len) | |
| 535 | + else begin | |
| 536 | + ml := m; ml.disp := ml.disp + 8; | |
| 537 | + AluM(aluCmp, RDX, ml); | |
| 538 | + end; | |
| 539 | + Check(ccBE); | |
| 540 | + Alu(aluCmp, RCX, RDX); | |
| 541 | + Check(ccBE); | |
| 542 | + end; | |
| 543 | + tmp := AllocTemp(16, 8); | |
| 544 | + MovRR(RAX, RDX); | |
| 545 | + Alu(aluSub, RAX, RCX); | |
| 546 | + Store(Mem(RBP, tmp + 8), RAX, 8); | |
| 547 | + if s <> 1 then ImulI(RCX, s); | |
| 548 | + case t^.form of | |
| 549 | + fPtr: MovRR(RAX, R8); | |
| 550 | + fArray: Lea(RAX, m); | |
| 551 | + else Load(RAX, m, 8, false); | |
| 552 | + end; | |
| 553 | + Alu(aluAdd, RAX, RCX); | |
| 554 | + Store(Mem(RBP, tmp), RAX, 8); | |
| 555 | + st := NewType(fSlice, 16, 8); | |
| 556 | + st^.base := t^.base; | |
| 557 | + TempItem(x, st, tmp); | |
| 558 | +end; | |
| 559 | + | |
| 560 | +{ Operators } | |
| 561 | + | |
| 562 | +procedure Neg(var x: TItem); | |
| 563 | +begin | |
| 564 | + if x.typ^.form = fReal then begin | |
| 565 | + if x.mode = mConst then x.val := x.val xor low(int64) | |
| 566 | + else begin Value(x); Btc(RAX, 63) end; | |
| 567 | + end else if IsInt(x.typ) then begin | |
| 568 | + if x.mode = mConst then x.val := Cut(-x.val, x.typ) | |
| 569 | + else begin Value(x); Unary(3, RAX); Norm(x.typ) end; | |
| 570 | + end else Mark('can''t negate ' + TypeName(x.typ)); | |
| 571 | +end; | |
| 572 | + | |
| 573 | +procedure NotOp(var x: TItem); | |
| 574 | +begin | |
| 575 | + if x.typ^.form = fBool then begin | |
| 576 | + if x.mode = mConst then x.val := 1 - x.val | |
| 577 | + else begin Value(x); AluI(aluXor, RAX, 1) end; | |
| 578 | + end else if IsInt(x.typ) then begin | |
| 579 | + if x.mode = mConst then x.val := Cut(not x.val, x.typ) | |
| 580 | + else begin Value(x); Unary(2, RAX); Norm(x.typ) end; | |
| 581 | + end else Mark('not needs BOOL or an integer'); | |
| 582 | +end; | |
| 583 | + | |
| 584 | +function IsStrLike(t: PType): boolean; | |
| 585 | +begin | |
| 586 | + IsStrLike := (t^.form = fStr) or ((t^.form in [fArray, fSlice]) and (t^.base = tU8)); | |
| 587 | +end; | |
| 588 | + | |
| 589 | +{ A []U8, [n]U8 or string as a slice in a temporary, for comparing. } | |
| 590 | +procedure StrSlice(var x: TItem); | |
| 591 | +var t: PType; | |
| 592 | +begin | |
| 593 | + t := NewType(fSlice, 16, 8); | |
| 594 | + t^.base := tU8; | |
| 595 | + if x.typ^.form = fSlice then begin | |
| 596 | + x.typ := t; CopyToTemp(x) | |
| 597 | + end else begin | |
| 598 | + if x.mode = mStr then begin Address(x, RAX); SliceOf(x, t, x.val) end | |
| 599 | + else begin | |
| 600 | + if x.vol then Mark('can''t compare through a volatile pointer'); | |
| 601 | + Address(x, RAX); SliceOf(x, t, x.typ^.len) | |
| 602 | + end; | |
| 603 | + end; | |
| 604 | +end; | |
| 605 | + | |
| 606 | +procedure Op1(op: TSym; var x: TItem; var L: longint); | |
| 607 | +var tmp: TItem; | |
| 608 | +begin | |
| 609 | + L := NoLabel; | |
| 610 | + if (op in [sAnd, sOr]) and (x.typ^.form = fBool) then begin | |
| 611 | + tmp := x; | |
| 612 | + Value(tmp); | |
| 613 | + if x.mode <> mConst then x := tmp; | |
| 614 | + TestRR(RAX, RAX); | |
| 615 | + if op = sAnd then Jcc(ccE, L) else Jcc(ccNE, L); | |
| 616 | + end else if (op in [sEql, sNeq]) and IsStrLike(x.typ) then | |
| 617 | + StrSlice(x) | |
| 618 | + else if x.mode in [mType, mBuiltin, mNone, mCase, mStr] then | |
| 619 | + Mark('not a value') | |
| 620 | + else if x.mode <> mConst then begin | |
| 621 | + Value(x); PushRax; x.mode := mStack | |
| 622 | + end; | |
| 623 | +end; | |
| 624 | + | |
| 625 | +function Log2(v: int64): longint; | |
| 626 | +var n: longint; | |
| 627 | +begin | |
| 628 | + n := 0; | |
| 629 | + while (v > 1) and (v and 1 = 0) do begin v := v shr 1; inc(n) end; | |
| 630 | + if v <> 1 then n := -1; | |
| 631 | + Log2 := n; | |
| 632 | +end; | |
| 633 | + | |
| 634 | +function FoldInt(op: TSym; a, b: int64; t: PType): int64; | |
| 635 | +var u: boolean; | |
| 636 | +begin | |
| 637 | + u := (t^.form = fInt) and not t^.signed; | |
| 638 | + case op of | |
| 639 | + sPlus: FoldInt := a + b; | |
| 640 | + sMinus: FoldInt := a - b; | |
| 641 | + sTimes: FoldInt := a * b; | |
| 642 | + sDiv, sMod: begin | |
| 643 | + if b = 0 then Mark('division by zero'); | |
| 644 | + if u then begin | |
| 645 | + if op = sDiv then FoldInt := int64(qword(a) div qword(b)) | |
| 646 | + else FoldInt := int64(qword(a) mod qword(b)); | |
| 647 | + end else if (b = -1) and (a = low(int64)) then Mark('division overflows') | |
| 648 | + else if op = sDiv then FoldInt := a div b | |
| 649 | + else FoldInt := a mod b; | |
| 650 | + end; | |
| 651 | + sAnd: FoldInt := a and b; | |
| 652 | + sOr: FoldInt := a or b; | |
| 653 | + sXor: FoldInt := a xor b; | |
| 654 | + sShl: FoldInt := a shl (b and 63); | |
| 655 | + sShr: if u then FoldInt := int64(qword(a) shr (b and 63)) | |
| 656 | + else FoldInt := SarInt64(a, b and 63); | |
| 657 | + sEql: FoldInt := ord(a = b); | |
| 658 | + sNeq: FoldInt := ord(a <> b); | |
| 659 | + sLss: if u then FoldInt := ord(qword(a) < qword(b)) else FoldInt := ord(a < b); | |
| 660 | + sLeq: if u then FoldInt := ord(qword(a) <= qword(b)) else FoldInt := ord(a <= b); | |
| 661 | + sGtr: if u then FoldInt := ord(qword(a) > qword(b)) else FoldInt := ord(a > b); | |
| 662 | + sGeq: if u then FoldInt := ord(qword(a) >= qword(b)) else FoldInt := ord(a >= b); | |
| 663 | + else Mark('bad operator'); FoldInt := 0; | |
| 664 | + end; | |
| 665 | +end; | |
| 666 | + | |
| 667 | +function FoldReal(op: TSym; const x, y: TItem; var r: TItem): boolean; | |
| 668 | +var a, b: double; | |
| 669 | +begin | |
| 670 | + a := RealOf(x); b := RealOf(y); | |
| 671 | + FoldReal := true; | |
| 672 | + case op of | |
| 673 | + sPlus: MakeReal(r, a + b); | |
| 674 | + sMinus: MakeReal(r, a - b); | |
| 675 | + sTimes: MakeReal(r, a * b); | |
| 676 | + sSlash: MakeReal(r, a / b); | |
| 677 | + sEql: MakeConst(r, tBool, ord(a = b)); | |
| 678 | + sNeq: MakeConst(r, tBool, ord(a <> b)); | |
| 679 | + sLss: MakeConst(r, tBool, ord(a < b)); | |
| 680 | + sLeq: MakeConst(r, tBool, ord(a <= b)); | |
| 681 | + sGtr: MakeConst(r, tBool, ord(a > b)); | |
| 682 | + sGeq: MakeConst(r, tBool, ord(a >= b)); | |
| 683 | + else FoldReal := false; | |
| 684 | + end; | |
| 685 | +end; | |
| 686 | + | |
| 687 | +procedure Op2(op: TSym; var x, y: TItem; L: longint); | |
| 688 | +const | |
| 689 | + Rel = [sEql, sNeq, sLss, sLeq, sGtr, sGeq]; | |
| 690 | + CcSigned: array[sEql..sGeq] of longint = (ccE, ccNE, ccL, ccLE, ccG, ccGE); | |
| 691 | + CcUnsigned: array[sEql..sGeq] of longint = (ccE, ccNE, ccB, ccBE, ccA, ccAE); | |
| 692 | +var | |
| 693 | + xt, yt, t: PType; r: TItem; scale, sh: longint; isPtr, unsign: boolean; | |
| 694 | +begin | |
| 695 | + xt := x.typ; yt := y.typ; | |
| 696 | + if y.mode in [mType, mBuiltin, mNone, mCase] then Mark('not a value'); | |
| 697 | + | |
| 698 | + if (op in [sAnd, sOr]) and (xt^.form = fBool) then begin | |
| 699 | + if yt^.form <> fBool then Mark('and, or need two BOOLs'); | |
| 700 | + if (x.mode = mConst) and (y.mode = mConst) then begin | |
| 701 | + FixLink(L); | |
| 702 | + if op = sAnd then x.val := x.val and y.val else x.val := x.val or y.val; | |
| 703 | + exit; | |
| 704 | + end; | |
| 705 | + Value(y); | |
| 706 | + FixLink(L); | |
| 707 | + x.mode := mReg; x.typ := tBool; | |
| 708 | + exit; | |
| 709 | + end; | |
| 710 | + | |
| 711 | + if (op in [sEql, sNeq]) and IsStrLike(xt) then begin | |
| 712 | + if not IsStrLike(yt) then Mark('can''t compare a string with ' + TypeName(yt)); | |
| 713 | + StrSlice(y); | |
| 714 | + Address(y, RSI); | |
| 715 | + Address(x, RDI); | |
| 716 | + if odd(depth) then AluI(aluSub, RSP, 8); | |
| 717 | + CallChunk(rtEq); | |
| 718 | + if odd(depth) then AluI(aluAdd, RSP, 8); | |
| 719 | + Extend(RAX, 1, false); | |
| 720 | + if op = sNeq then AluI(aluXor, RAX, 1); | |
| 721 | + MakeConst(x, tBool, 0); | |
| 722 | + x.mode := mReg; | |
| 723 | + exit; | |
| 724 | + end; | |
| 725 | + | |
| 726 | + { the type of the result, and the checks } | |
| 727 | + t := nil; scale := 1; isPtr := false; | |
| 728 | + if IsInt(xt) and IsInt(yt) then begin | |
| 729 | + if op = sSlash then Mark('/ is for F64; integers use div'); | |
| 730 | + if op in [sShl, sShr] then begin | |
| 731 | + if xt^.form = fConst then t := tConst else t := xt | |
| 732 | + end else if (xt^.form = fConst) and (yt^.form = fConst) then t := tConst | |
| 733 | + else if xt^.form = fConst then begin | |
| 734 | + if (x.mode = mConst) and not Fits(x.val, yt) then Mark('constant doesn''t fit ' + TypeName(yt)); | |
| 735 | + t := yt | |
| 736 | + end else if yt^.form = fConst then begin | |
| 737 | + if (y.mode = mConst) and not Fits(y.val, xt) then Mark('constant doesn''t fit ' + TypeName(xt)); | |
| 738 | + t := xt | |
| 739 | + end else if Same(xt, yt) then t := xt | |
| 740 | + else Mark('different types: ' + TypeName(xt) + ' and ' + TypeName(yt)); | |
| 741 | + end else if (xt^.form = fReal) and (yt^.form = fReal) then begin | |
| 742 | + if not (op in Rel + [sPlus, sMinus, sTimes, sSlash]) then Mark('not an F64 operator'); | |
| 743 | + t := tReal; | |
| 744 | + end else if (xt^.form = fBool) and (yt^.form = fBool) then begin | |
| 745 | + if not (op in [sXor, sEql, sNeq]) then Mark('not a BOOL operator'); | |
| 746 | + t := tBool; | |
| 747 | + end else if (xt^.form = fPtr) and IsInt(yt) and (op in [sPlus, sMinus]) then begin | |
| 748 | + t := xt; scale := xt^.base^.size; isPtr := true; | |
| 749 | + if y.mode = mConst then y.typ := tI64; | |
| 750 | + end else if (xt^.form = fPtr) and (yt^.form = fPtr) and (op = sMinus) then begin | |
| 751 | + if not Same(xt, yt) then Mark('different pointer types'); | |
| 752 | + t := tI64; scale := xt^.base^.size; isPtr := true; | |
| 753 | + end else if (xt^.form in [fPtr, fProc, fNil]) and (yt^.form in [fPtr, fProc, fNil]) then begin | |
| 754 | + if not (op in Rel) then Mark('not a pointer operator'); | |
| 755 | + if (xt^.form = fProc) or (yt^.form = fProc) then | |
| 756 | + if not (op in [sEql, sNeq]) then Mark('procedures compare only with = and #'); | |
| 757 | + if (xt^.form <> fNil) and (yt^.form <> fNil) and not Same(xt, yt) then | |
| 758 | + if not ((xt^.form = fPtr) and (yt^.form = fPtr) and Same(xt^.base, yt^.base)) then | |
| 759 | + Mark('different pointer types'); | |
| 760 | + t := tU64; | |
| 761 | + end else | |
| 762 | + Mark('can''t combine ' + TypeName(xt) + ' and ' + TypeName(yt)); | |
| 763 | + unsign := (t^.form = fInt) and not t^.signed; | |
| 764 | + | |
| 765 | + { constants } | |
| 766 | + if (x.mode = mConst) and (y.mode = mConst) and not isPtr then begin | |
| 767 | + if t^.form = fReal then begin | |
| 768 | + if not FoldReal(op, x, y, r) then Mark('not an F64 operator'); | |
| 769 | + x := r; | |
| 770 | + end else if op in Rel then | |
| 771 | + MakeConst(x, tBool, FoldInt(op, x.val, y.val, t)) | |
| 772 | + else | |
| 773 | + MakeConst(x, t, Cut(FoldInt(op, x.val, y.val, t), t)); | |
| 774 | + exit; | |
| 775 | + end; | |
| 776 | + | |
| 777 | + { x into rax, y into rcx } | |
| 778 | + if y.mode = mConst then begin | |
| 779 | + if x.mode = mStack then PopR(RAX) else Value(x); | |
| 780 | + MovRI(RCX, y.val); | |
| 781 | + end else begin | |
| 782 | + Value(y); | |
| 783 | + MovRR(RCX, RAX); | |
| 784 | + if x.mode = mConst then MovRI(RAX, x.val) else PopR(RAX); | |
| 785 | + end; | |
| 786 | + | |
| 787 | + if t^.form = fReal then begin | |
| 788 | + MovqXR(0, RAX); | |
| 789 | + MovqXR(1, RCX); | |
| 790 | + case op of | |
| 791 | + sPlus: Sse($58, 0, 1); | |
| 792 | + sMinus: Sse($5C, 0, 1); | |
| 793 | + sTimes: Sse($59, 0, 1); | |
| 794 | + sSlash: Sse($5E, 0, 1); | |
| 795 | + sEql: begin Ucomisd(0, 1); SetCC(ccE, RAX); SetCC(ccNP, RCX); Alu(aluAnd, RAX, RCX) end; | |
| 796 | + sNeq: begin Ucomisd(0, 1); SetCC(ccNE, RAX); SetCC(ccP, RCX); Alu(aluOr, RAX, RCX) end; | |
| 797 | + sLss: begin Ucomisd(1, 0); SetCC(ccA, RAX) end; | |
| 798 | + sLeq: begin Ucomisd(1, 0); SetCC(ccAE, RAX) end; | |
| 799 | + sGtr: begin Ucomisd(0, 1); SetCC(ccA, RAX) end; | |
| 800 | + sGeq: begin Ucomisd(0, 1); SetCC(ccAE, RAX) end; | |
| 801 | + end; | |
| 802 | + if not (op in Rel) then MovqRX(RAX, 0); | |
| 803 | + end else if op in Rel then begin | |
| 804 | + Alu(aluCmp, RAX, RCX); | |
| 805 | + if (t^.form = fInt) and t^.signed or (t^.form = fConst) then SetCC(CcSigned[op], RAX) | |
| 806 | + else SetCC(CcUnsigned[op], RAX); | |
| 807 | + end else begin | |
| 808 | + case op of | |
| 809 | + sPlus: begin if scale <> 1 then ImulI(RCX, scale); Alu(aluAdd, RAX, RCX) end; | |
| 810 | + sMinus: begin | |
| 811 | + if (scale <> 1) and (t <> tI64) then ImulI(RCX, scale); | |
| 812 | + Alu(aluSub, RAX, RCX); | |
| 813 | + if (t = tI64) and isPtr and (scale <> 1) then begin | |
| 814 | + sh := Log2(scale); | |
| 815 | + if sh >= 0 then ShiftI(shSar, RAX, sh) | |
| 816 | + else begin MovRI(RCX, scale); Cqo; Unary(7, RCX) end; | |
| 817 | + end; | |
| 818 | + end; | |
| 819 | + sTimes: Imul(RAX, RCX); | |
| 820 | + sDiv, sMod: begin | |
| 821 | + if unsign then begin MovRI(RDX, 0); Unary(6, RCX) end | |
| 822 | + else begin Cqo; Unary(7, RCX) end; | |
| 823 | + if op = sMod then MovRR(RAX, RDX); | |
| 824 | + end; | |
| 825 | + sAnd: Alu(aluAnd, RAX, RCX); | |
| 826 | + sOr: Alu(aluOr, RAX, RCX); | |
| 827 | + sXor: Alu(aluXor, RAX, RCX); | |
| 828 | + sShl: Shift(shShl, RAX); | |
| 829 | + sShr: if unsign then Shift(shShr, RAX) else Shift(shSar, RAX); | |
| 830 | + else Mark('not an operator for ' + TypeName(t)); | |
| 831 | + end; | |
| 832 | + Norm(t); | |
| 833 | + end; | |
| 834 | + x.mode := mReg; | |
| 835 | + if op in Rel then x.typ := tBool else x.typ := t; | |
| 836 | +end; | |
| 837 | + | |
| 838 | +procedure Convert(var x: TItem; t: PType); | |
| 839 | +var s: PType; r: double; L1, L2: longint; | |
| 840 | +begin | |
| 841 | + s := x.typ; | |
| 842 | + if x.mode in [mType, mBuiltin, mNone, mCase, mStr] then Mark('can''t convert this'); | |
| 843 | + if Same(s, t) then exit; | |
| 844 | + if IsInt(s) and (t^.form = fInt) then begin | |
| 845 | + if x.mode = mConst then x.val := Cut(x.val, t) | |
| 846 | + else begin Value(x); Norm(t) end; | |
| 847 | + end else if IsInt(s) and (t^.form = fReal) then begin | |
| 848 | + if x.mode = mConst then begin | |
| 849 | + if (s^.form = fInt) and not s^.signed then r := qword(x.val) else r := x.val; | |
| 850 | + MakeReal(x, r); | |
| 851 | + end else begin | |
| 852 | + Value(x); | |
| 853 | + if (s^.form = fInt) and not s^.signed and (s^.size = 8) then begin | |
| 854 | + { above high(I64): halve, keeping the low bit, and double } | |
| 855 | + L1 := NoLabel; L2 := NoLabel; | |
| 856 | + TestRR(RAX, RAX); | |
| 857 | + Jcc(ccS, L1); | |
| 858 | + Cvtsi2sd(0, RAX); | |
| 859 | + Jmp(L2); | |
| 860 | + FixLink(L1); | |
| 861 | + MovRR(RCX, RAX); | |
| 862 | + ShiftI(shShr, RCX, 1); | |
| 863 | + AluI(aluAnd, RAX, 1); | |
| 864 | + Alu(aluOr, RCX, RAX); | |
| 865 | + Cvtsi2sd(0, RCX); | |
| 866 | + Sse($58, 0, 0); | |
| 867 | + FixLink(L2); | |
| 868 | + end else Cvtsi2sd(0, RAX); | |
| 869 | + MovqRX(RAX, 0); | |
| 870 | + end; | |
| 871 | + end else if (s^.form = fReal) and (t^.form = fInt) then begin | |
| 872 | + if x.mode = mConst then begin | |
| 873 | + r := RealOf(x); | |
| 874 | + if (r <> r) or (r >= 9.2233720368547758e18) or (r < -9.2233720368547758e18) then | |
| 875 | + x.val := Cut(low(int64), t) | |
| 876 | + else x.val := Cut(trunc(r), t); | |
| 877 | + end else begin | |
| 878 | + Value(x); MovqXR(0, RAX); Cvttsd2si(RAX, 0); Norm(t) | |
| 879 | + end; | |
| 880 | + end else if (IsInt(s) or (s^.form in [fPtr, fProc, fNil])) and (t^.form in [fPtr, fProc]) then begin | |
| 881 | + if x.mode <> mConst then Value(x); | |
| 882 | + end else if (s^.form in [fPtr, fProc]) and (t^.form = fInt) then begin | |
| 883 | + Value(x); Norm(t) | |
| 884 | + end else | |
| 885 | + Mark('can''t convert ' + TypeName(s) + ' to ' + TypeName(t)); | |
| 886 | + x.typ := t; | |
| 887 | +end; | |
| 888 | + | |
| 889 | +{ Calls } | |
| 890 | + | |
| 891 | +procedure BeginCall(var c: TCall; var x: TItem); | |
| 892 | +begin | |
| 893 | + if x.mode = mProc then begin | |
| 894 | + c.sig := x.obj^.typ; c.chunk := x.obj^.chunk; c.target := 0 | |
| 895 | + end else begin | |
| 896 | + if x.typ^.form <> fProc then Mark('not a procedure'); | |
| 897 | + c.sig := x.typ; c.chunk := -1; | |
| 898 | + c.target := ToSlot(x); | |
| 899 | + end; | |
| 900 | + c.param := c.sig^.fields; | |
| 901 | + c.n := 0; | |
| 902 | +end; | |
| 903 | + | |
| 904 | +procedure PassArg(var c: TCall; var a: TItem); | |
| 905 | +var p: PObj; s: int64; | |
| 906 | +begin | |
| 907 | + p := c.param; | |
| 908 | + if p = nil then Mark('too many arguments'); | |
| 909 | + if c.n > High(c.slots) then Mark('too many arguments'); | |
| 910 | + if p^.isVar then begin | |
| 911 | + if (a.mode <> mVar) or not a.isVar then Mark('a var parameter needs a variable'); | |
| 912 | + if not Same(a.typ, p^.typ) then Mark('a var parameter needs exactly ' + TypeName(p^.typ)); | |
| 913 | + if a.vol then Mark('can''t pass through a volatile pointer to a var parameter'); | |
| 914 | + Address(a, RAX); | |
| 915 | + s := AllocTemp(8, 8); | |
| 916 | + Store(Mem(RBP, s), RAX, 8); | |
| 917 | + end else if IsAggregate(p^.typ) then begin | |
| 918 | + Coerce(a, p^.typ); | |
| 919 | + if a.isVar or a.vol or (a.base <> bFrame) then CopyToTemp(a); | |
| 920 | + Address(a, RAX); | |
| 921 | + s := AllocTemp(8, 8); | |
| 922 | + Store(Mem(RBP, s), RAX, 8); | |
| 923 | + end else begin | |
| 924 | + Coerce(a, p^.typ); | |
| 925 | + s := ToSlot(a); | |
| 926 | + end; | |
| 927 | + c.slots[c.n] := s; | |
| 928 | + inc(c.n); | |
| 929 | + c.param := p^.next; | |
| 930 | +end; | |
| 931 | + | |
| 932 | +procedure EndCall(var c: TCall; var x: TItem); | |
| 933 | +var | |
| 934 | + p: PObj; res: PType; i, ni, nf, k, pad: longint; ret: int64; | |
| 935 | + isF: array[0..63] of boolean; onStack: array[0..63] of boolean; | |
| 936 | +begin | |
| 937 | + if c.param <> nil then Mark('too few arguments'); | |
| 938 | + res := c.sig^.base; | |
| 939 | + ret := 0; | |
| 940 | + ni := 0; nf := 0; | |
| 941 | + if (res <> nil) and IsAggregate(res) then begin | |
| 942 | + ret := AllocTemp(res^.size, res^.align); | |
| 943 | + ni := 1; | |
| 944 | + end; | |
| 945 | + { the System V classes: each argument in the next free register of | |
| 946 | + its kind, or else on the stack } | |
| 947 | + p := c.sig^.fields; | |
| 948 | + k := 0; | |
| 949 | + for i := 0 to c.n - 1 do begin | |
| 950 | + isF[i] := (p^.typ^.form = fReal) and not p^.isVar; | |
| 951 | + if isF[i] then begin onStack[i] := nf >= 8; if nf < 8 then inc(nf) end | |
| 952 | + else begin onStack[i] := ni >= 6; if ni < 6 then inc(ni) end; | |
| 953 | + if onStack[i] then inc(k); | |
| 954 | + p := p^.next; | |
| 955 | + end; | |
| 956 | + pad := ord(odd(depth + k)); | |
| 957 | + if pad <> 0 then AluI(aluSub, RSP, 8); | |
| 958 | + for i := c.n - 1 downto 0 do | |
| 959 | + if onStack[i] then begin | |
| 960 | + Load(RAX, Mem(RBP, c.slots[i]), 8, false); | |
| 961 | + Push(RAX); | |
| 962 | + end; | |
| 963 | + ni := 0; nf := 0; | |
| 964 | + if ret <> 0 then begin Lea(RDI, Mem(RBP, ret)); ni := 1 end; | |
| 965 | + for i := 0 to c.n - 1 do | |
| 966 | + if not onStack[i] then | |
| 967 | + if isF[i] then begin MovsdXM(nf, Mem(RBP, c.slots[i])); inc(nf) end | |
| 968 | + else begin Load(IntRegs[ni], Mem(RBP, c.slots[i]), 8, false); inc(ni) end; | |
| 969 | + if c.chunk >= 0 then CallChunk(c.chunk) | |
| 970 | + else begin | |
| 971 | + Load(R11, Mem(RBP, c.target), 8, false); | |
| 972 | + CallR(R11); | |
| 973 | + end; | |
| 974 | + if k + pad > 0 then AluI(aluAdd, RSP, 8 * (k + pad)); | |
| 975 | + FillChar(x, SizeOf(x), 0); | |
| 976 | + if res = nil then begin | |
| 977 | + x.mode := mNone; x.typ := tNone | |
| 978 | + end else if ret <> 0 then | |
| 979 | + TempItem(x, res, ret) | |
| 980 | + else begin | |
| 981 | + x.mode := mReg; x.typ := res; | |
| 982 | + if res^.form = fReal then MovqRX(RAX, 0) | |
| 983 | + else if res^.form = fBool then Extend(RAX, 1, false) | |
| 984 | + else Norm(res); | |
| 985 | + end; | |
| 986 | +end; | |
| 987 | + | |
| 988 | +{ Variants } | |
| 989 | + | |
| 990 | +procedure Construct(var x: TItem; c: PObj); | |
| 991 | +var t: PType; tmp: int64; | |
| 992 | +begin | |
| 993 | + t := c^.typ; | |
| 994 | + tmp := AllocTemp(t^.size, t^.align); | |
| 995 | + Lea(RDI, Mem(RBP, tmp)); | |
| 996 | + MovRI(RCX, t^.size); | |
| 997 | + MovRI(RAX, 0); | |
| 998 | + RepStosb; | |
| 999 | + MovRI(RAX, c^.val); | |
| 1000 | + Store(Mem(RBP, tmp), RAX, 8); | |
| 1001 | + TempItem(x, t, tmp); | |
| 1002 | +end; | |
| 1003 | + | |
| 1004 | +procedure SetField(const x: TItem; f: PObj; var a: TItem); | |
| 1005 | +begin | |
| 1006 | + Coerce(a, f^.typ); | |
| 1007 | + if IsScalar(f^.typ) then begin | |
| 1008 | + Value(a); | |
| 1009 | + Store(Mem(RBP, x.off + f^.val), RAX, ScalarSize(f^.typ)); | |
| 1010 | + end else begin | |
| 1011 | + if a.vol then Mark('can''t copy through a volatile pointer'); | |
| 1012 | + Address(a, RSI); | |
| 1013 | + Lea(RDI, Mem(RBP, x.off + f^.val)); | |
| 1014 | + Copy(f^.typ^.size); | |
| 1015 | + end; | |
| 1016 | +end; | |
| 1017 | + | |
| 1018 | +function MatchBegin(var x: TItem): int64; | |
| 1019 | +var p: int64; | |
| 1020 | +begin | |
| 1021 | + if (x.mode <> mVar) or (x.typ^.form <> fVariant) then Mark('match needs a variant'); | |
| 1022 | + Address(x, RAX); | |
| 1023 | + p := AllocTemp(8, 8); | |
| 1024 | + Store(Mem(RBP, p), RAX, 8); | |
| 1025 | + MatchBegin := p; | |
| 1026 | +end; | |
| 1027 | + | |
| 1028 | +procedure MatchTag(p: int64; tag: int64; var L: longint); | |
| 1029 | +begin | |
| 1030 | + Load(RAX, Mem(RBP, p), 8, false); | |
| 1031 | + Load(RAX, Mem(RAX, 0), 8, false); | |
| 1032 | + CmpI(RAX, tag); | |
| 1033 | + Jcc(ccNE, L); | |
| 1034 | +end; | |
| 1035 | + | |
| 1036 | +{ The end of a round of a for loop: stop if the step would pass the | |
| 1037 | + limit, else step. Counting the distance left, rather than comparing | |
| 1038 | + after the step, can't be fooled by the variable wrapping around. } | |
| 1039 | +procedure ForNext(o: PObj; bslot, step: int64; var exitL: longint; top: longint); | |
| 1040 | +var v, w: TItem; | |
| 1041 | +begin | |
| 1042 | + MakeItem(w, o); | |
| 1043 | + Value(w); | |
| 1044 | + Load(RCX, Mem(RBP, bslot), 8, false); | |
| 1045 | + if step > 0 then begin | |
| 1046 | + Alu(aluSub, RCX, RAX); | |
| 1047 | + CmpI(RCX, step); | |
| 1048 | + end else begin | |
| 1049 | + Alu(aluSub, RAX, RCX); | |
| 1050 | + CmpI(RAX, -step); | |
| 1051 | + end; | |
| 1052 | + Jcc(ccB, exitL); | |
| 1053 | + MakeItem(v, o); | |
| 1054 | + Save(v); | |
| 1055 | + MakeItem(w, o); | |
| 1056 | + Value(w); | |
| 1057 | + MovRI(RCX, step); | |
| 1058 | + Alu(aluAdd, RAX, RCX); | |
| 1059 | + Norm(o^.typ); | |
| 1060 | + Assign(v, w); | |
| 1061 | + JmpBack(top); | |
| 1062 | +end; | |
| 1063 | + | |
| 1064 | +procedure CondJump(var x: TItem; var L: longint); | |
| 1065 | +begin | |
| 1066 | + if x.typ^.form <> fBool then Mark('a condition is a BOOL'); | |
| 1067 | + Value(x); | |
| 1068 | + TestRR(RAX, RAX); | |
| 1069 | + Jcc(ccE, L); | |
| 1070 | +end; | |
| 1071 | + | |
| 1072 | +{ Procedures } | |
| 1073 | + | |
| 1074 | +procedure BeginProc(sig: PType; params: PObj); | |
| 1075 | +var p: PObj; ni, nf: longint; stackOff: int64; f: boolean; | |
| 1076 | +begin | |
| 1077 | + frameTop := 0; frameMax := 0; permTop := 0; depth := 0; | |
| 1078 | + exitL := NoLabel; | |
| 1079 | + numIn := 0; | |
| 1080 | + result := sig^.base; | |
| 1081 | + ni := 0; nf := 0; stackOff := 16; | |
| 1082 | + if (result <> nil) and IsAggregate(result) then begin | |
| 1083 | + retSlot := AllocPerm(8, 8); | |
| 1084 | + inReg[0] := RDI; inSlot[0] := retSlot; inSize[0] := 8; | |
| 1085 | + numIn := 1; ni := 1; | |
| 1086 | + end; | |
| 1087 | + p := params; | |
| 1088 | + while p <> nil do begin | |
| 1089 | + p^.ind := p^.isVar or IsAggregate(p^.typ); | |
| 1090 | + f := (p^.typ^.form = fReal) and not p^.isVar; | |
| 1091 | + if (f and (nf < 8)) or (not f and (ni < 6)) then begin | |
| 1092 | + p^.val := AllocPerm(8, 8); | |
| 1093 | + if f then begin inReg[numIn] := -1 - nf; inc(nf) end | |
| 1094 | + else begin inReg[numIn] := IntRegs[ni]; inc(ni) end; | |
| 1095 | + inSlot[numIn] := p^.val; | |
| 1096 | + if p^.ind then inSize[numIn] := 8 else inSize[numIn] := ScalarSize(p^.typ); | |
| 1097 | + inc(numIn); | |
| 1098 | + end else begin | |
| 1099 | + p^.val := stackOff; | |
| 1100 | + inc(stackOff, 8); | |
| 1101 | + end; | |
| 1102 | + p := p^.next; | |
| 1103 | + end; | |
| 1104 | +end; | |
| 1105 | + | |
| 1106 | +procedure EnterBody(c: longint); | |
| 1107 | +var i, top: longint; | |
| 1108 | +begin | |
| 1109 | + BeginChunk(c); | |
| 1110 | + Push(RBP); | |
| 1111 | + MovRR(RBP, RSP); | |
| 1112 | + Put1($48); Put1($81); Put1($EC); { sub rsp, imm32 } | |
| 1113 | + frameFix := pc; Put4(0); | |
| 1114 | + { zero the frame, a quadword at a time: r10 counts up to zero } | |
| 1115 | + Put1($49); Put1($C7); Put1($C2); { mov r10, imm32 } | |
| 1116 | + zeroFix := pc; Put4(0); | |
| 1117 | + MovRI(RAX, 0); | |
| 1118 | + top := pc; | |
| 1119 | + Store(MemIdx(RBP, R10, 8, 0), RAX, 8); | |
| 1120 | + AluI(aluAdd, R10, 1); | |
| 1121 | + JccBack(ccNE, top); | |
| 1122 | + for i := 0 to numIn - 1 do | |
| 1123 | + if inReg[i] < 0 then MovsdMX(Mem(RBP, inSlot[i]), -1 - inReg[i]) | |
| 1124 | + else Store(Mem(RBP, inSlot[i]), inReg[i], inSize[i]); | |
| 1125 | +end; | |
| 1126 | + | |
| 1127 | +procedure ReturnValue(var x: TItem); | |
| 1128 | +begin | |
| 1129 | + if result = nil then Mark('this procedure returns nothing'); | |
| 1130 | + Coerce(x, result); | |
| 1131 | + if IsAggregate(result) then begin | |
| 1132 | + if x.vol then Mark('can''t copy through a volatile pointer'); | |
| 1133 | + Address(x, RSI); | |
| 1134 | + Load(RDI, Mem(RBP, retSlot), 8, false); | |
| 1135 | + Copy(result^.size); | |
| 1136 | + Load(RAX, Mem(RBP, retSlot), 8, false); | |
| 1137 | + end else begin | |
| 1138 | + Value(x); | |
| 1139 | + if result^.form = fReal then MovqXR(0, RAX); | |
| 1140 | + end; | |
| 1141 | + Jmp(exitL); | |
| 1142 | +end; | |
| 1143 | + | |
| 1144 | +procedure ReturnNone; | |
| 1145 | +begin | |
| 1146 | + if result <> nil then Mark('return needs a result'); | |
| 1147 | + Jmp(exitL); | |
| 1148 | +end; | |
| 1149 | + | |
| 1150 | +procedure EndProc(c: longint); | |
| 1151 | +var n: int64; | |
| 1152 | +begin | |
| 1153 | + if depth <> 0 then Fatal('internal error: stack depth'); | |
| 1154 | + { running off the end returns zeros } | |
| 1155 | + if result <> nil then | |
| 1156 | + if IsAggregate(result) then begin | |
| 1157 | + Load(RDI, Mem(RBP, retSlot), 8, false); | |
| 1158 | + MovRI(RCX, result^.size); | |
| 1159 | + MovRI(RAX, 0); | |
| 1160 | + RepStosb; | |
| 1161 | + Load(RAX, Mem(RBP, retSlot), 8, false); | |
| 1162 | + end else begin | |
| 1163 | + MovRI(RAX, 0); | |
| 1164 | + MovqXR(0, RAX); | |
| 1165 | + end; | |
| 1166 | + FixLink(exitL); | |
| 1167 | + Leave; | |
| 1168 | + Ret; | |
| 1169 | + n := AlignUp(frameMax, 16); | |
| 1170 | + if n < 16 then n := 16; | |
| 1171 | + if n > high(longint) then Mark('frame too big'); | |
| 1172 | + Put4At(frameFix, n); | |
| 1173 | + Put4At(zeroFix, -(n div 8)); | |
| 1174 | + EndChunk(c); | |
| 1175 | +end; | |
| 1176 | + | |
| 1177 | +end. |
| @@ -0,0 +1,111 @@ | ||
| 1 | +{ jed.pas: the command line. | |
| 2 | + | |
| 3 | + jed [-I dir]... [-o file] Main.jed | |
| 4 | + | |
| 5 | + Compiles the main module and every module it imports, writes the | |
| 6 | + entry point, links what it reaches, and writes the executable: by | |
| 7 | + default the main module's file without its .jed. | |
| 8 | + | |
| 9 | + The entry point saves argc and argv, runs every module's body (those | |
| 10 | + of the modules the program imports, imported modules first), then | |
| 11 | + the main module's main, and exits with its status. | |
| 12 | + | |
| 13 | + Depends on: everything else. } | |
| 14 | + | |
| 15 | +program jed; | |
| 16 | + | |
| 17 | +{$mode objfpc}{$H-} | |
| 18 | + | |
| 19 | +uses math, scan, sym, x64, gen, parse, link, elf; | |
| 20 | + | |
| 21 | +{ The modules the program uses: the main module, Rt, and every module | |
| 22 | + they import. A module read only for the interface check is not. } | |
| 23 | +procedure Use(m: PModule); | |
| 24 | +var i: PImport; | |
| 25 | +begin | |
| 26 | + if m^.used then exit; | |
| 27 | + m^.used := true; | |
| 28 | + i := m^.imports; | |
| 29 | + while i <> nil do begin | |
| 30 | + Use(i^.module); | |
| 31 | + i := i^.next; | |
| 32 | + end; | |
| 33 | +end; | |
| 34 | + | |
| 35 | +var | |
| 36 | + path, outPath, dir, s: shortstring; | |
| 37 | + i, entry: longint; | |
| 38 | + m, rt: PModule; | |
| 39 | + main: PObj; | |
| 40 | + hasResult: boolean; | |
| 41 | + | |
| 42 | +begin | |
| 43 | + { constants fold as the machine computes: NaN and infinities, no traps } | |
| 44 | + SetExceptionMask([exInvalidOp, exDenormalized, exZeroDivide, exOverflow, exUnderflow, exPrecision]); | |
| 45 | + path := ''; | |
| 46 | + outPath := ''; | |
| 47 | + numInc := 0; | |
| 48 | + i := 1; | |
| 49 | + while i <= ParamCount do begin | |
| 50 | + s := ParamStr(i); | |
| 51 | + if s = '-I' then begin | |
| 52 | + inc(i); | |
| 53 | + if numInc > High(incDirs) then Fatal('too many -I'); | |
| 54 | + incDirs[numInc] := ParamStr(i); | |
| 55 | + inc(numInc); | |
| 56 | + end else if s = '-o' then begin | |
| 57 | + inc(i); | |
| 58 | + outPath := ParamStr(i); | |
| 59 | + end else if (s <> '') and (s[1] = '-') then | |
| 60 | + Fatal('unknown option ' + s) | |
| 61 | + else path := s; | |
| 62 | + inc(i); | |
| 63 | + end; | |
| 64 | + if path = '' then Fatal('usage: jed [-I dir]... [-o file] Main.jed'); | |
| 65 | + dir := ParamStr(0); | |
| 66 | + i := length(dir); | |
| 67 | + while (i > 0) and (dir[i] <> '/') do dec(i); | |
| 68 | + if i = 0 then libDir := 'lib' else libDir := copy(dir, 1, i) + 'lib'; | |
| 69 | + if outPath = '' then begin | |
| 70 | + outPath := path; | |
| 71 | + if copy(outPath, length(outPath) - 3, 4) = '.jed' then | |
| 72 | + outPath := copy(outPath, 1, length(outPath) - 4) | |
| 73 | + else outPath := outPath + '.out'; | |
| 74 | + end; | |
| 75 | + | |
| 76 | + m := CompileMain(path); | |
| 77 | + | |
| 78 | + Use(m); | |
| 79 | + rt := firstModule; | |
| 80 | + while (rt <> nil) and (NameOf(rt^.name) <> 'Rt') do rt := rt^.next; | |
| 81 | + if rt <> nil then Use(rt); | |
| 82 | + | |
| 83 | + main := Find(m^.scope, Intern('main', 4)); | |
| 84 | + hasResult := false; | |
| 85 | + if main <> nil then begin | |
| 86 | + if (main^.cls <> oProc) or not main^.exported then Fatal('main must be an exported procedure'); | |
| 87 | + if (main^.typ^.len <> 0) or ((main^.typ^.base <> nil) and (main^.typ^.base <> tI64)) then | |
| 88 | + Fatal('main must be proc main* or proc main*: I64'); | |
| 89 | + hasResult := main^.typ^.base <> nil; | |
| 90 | + end; | |
| 91 | + | |
| 92 | + entry := NewChunk(secText, -1, Intern('_start', 6)); | |
| 93 | + BeginChunk(entry); | |
| 94 | + Load(RAX, Mem(RSP, 0), 8, false); | |
| 95 | + Store(MemRip(argcChunk, 0), RAX, 8); | |
| 96 | + Lea(RAX, Mem(RSP, 8)); | |
| 97 | + Store(MemRip(argvChunk, 0), RAX, 8); | |
| 98 | + m := firstModule; | |
| 99 | + while m <> nil do begin | |
| 100 | + if m^.used and (m^.body >= 0) then CallChunk(m^.body); | |
| 101 | + m := m^.next; | |
| 102 | + end; | |
| 103 | + if main <> nil then CallChunk(main^.chunk); | |
| 104 | + if hasResult then MovRR(RDI, RAX) else MovRI(RDI, 0); | |
| 105 | + MovRI(RAX, 231); { exit_group } | |
| 106 | + Syscall; | |
| 107 | + EndChunk(entry); | |
| 108 | + | |
| 109 | + LinkAll(entry, HeaderSize); | |
| 110 | + WriteElf(outPath, entry); | |
| 111 | +end. |
| @@ -0,0 +1,1347 @@ | ||
| 1 | +{ parse.pas: the parser. | |
| 2 | + | |
| 3 | + Reads modules by recursive descent and has gen.pas emit code as it | |
| 4 | + goes: one pass, no tree. Importing a module that isn't compiled yet | |
| 5 | + compiles it there and then, so modules are finished in an order where | |
| 6 | + each comes after the modules it imports; that is the order in which | |
| 7 | + their bodies run. | |
| 8 | + | |
| 9 | + A module may come in one file per target (Name.linux-x86_64.jed and | |
| 10 | + so on). The file for this target is compiled; the others beside it | |
| 11 | + are read for their declarations only, and must export the same | |
| 12 | + interface. | |
| 13 | + | |
| 14 | + Depends on: scan, tok, x64, gen. | |
| 15 | + Leaves out (for later stages): multiple results, errors, enumerations | |
| 16 | + and sets, record literals, defer, NEW, generic modules, extern. } | |
| 17 | + | |
| 18 | +unit parse; | |
| 19 | + | |
| 20 | +{$mode objfpc}{$H-} | |
| 21 | + | |
| 22 | +interface | |
| 23 | + | |
| 24 | +uses scan, sym, x64, gen; | |
| 25 | + | |
| 26 | +const | |
| 27 | + Target = 'linux-x86_64'; | |
| 28 | + TargetOS = 'linux'; | |
| 29 | + TargetArch = 'x86_64'; | |
| 30 | + | |
| 31 | +var | |
| 32 | + libDir, mainDir: shortstring; | |
| 33 | + incDirs: array[0..15] of shortstring; | |
| 34 | + numInc: longint; | |
| 35 | + firstModule, lastModule: PModule; { in the order they were finished } | |
| 36 | + | |
| 37 | +function CompileMain(const path: shortstring): PModule; | |
| 38 | +function ImportModule(name: longint): PModule; | |
| 39 | + | |
| 40 | +implementation | |
| 41 | + | |
| 42 | +uses BaseUnix; | |
| 43 | + | |
| 44 | +var | |
| 45 | + checkMode: boolean; { reading a file only for its interface } | |
| 46 | + inTypes: boolean; { in a type section: ^T may name a later type } | |
| 47 | + fwdTypes: array[0..255] of PType; | |
| 48 | + numFwd: longint; | |
| 49 | + declExported: boolean; { the export mark of the declaration being read } | |
| 50 | + curBreak: PLongint; { the break chain of the innermost loop, or nil } | |
| 51 | + curResult: PType; { the result type of the procedure being compiled } | |
| 52 | + tArgv, tBodySig: PType; | |
| 53 | + nameRt, nameFail, nameEq, nameMain, nameBody: longint; | |
| 54 | + | |
| 55 | +procedure Expression(var x: TItem; want: PType); forward; | |
| 56 | +function ParseType: PType; forward; | |
| 57 | +procedure Statements; forward; | |
| 58 | + | |
| 59 | +{ Little helpers } | |
| 60 | + | |
| 61 | +procedure Expect(s: TSym; const what: shortstring); | |
| 62 | +begin | |
| 63 | + if tok <> s then Mark(what + ' expected'); | |
| 64 | + Next; | |
| 65 | +end; | |
| 66 | + | |
| 67 | +function Name(const s: shortstring): longint; | |
| 68 | +begin | |
| 69 | + Name := Intern(@s[1], length(s)); | |
| 70 | +end; | |
| 71 | + | |
| 72 | +procedure IdentDef(var n: longint; var exp: boolean); | |
| 73 | +begin | |
| 74 | + if tok <> sIdent then Mark('name expected'); | |
| 75 | + n := id; | |
| 76 | + Next; | |
| 77 | + exp := false; | |
| 78 | + if tok = sTimes then begin | |
| 79 | + exp := true; | |
| 80 | + Next; | |
| 81 | + end; | |
| 82 | +end; | |
| 83 | + | |
| 84 | +{ Declares o in the innermost scope; a module's own declarations are | |
| 85 | + also kept in order, for the interface check. } | |
| 86 | +procedure Declare(o: PObj); | |
| 87 | +begin | |
| 88 | + Insert(o, TopScope); | |
| 89 | + if TopScope = curModule^.scope then begin | |
| 90 | + if curModule^.last = nil then curModule^.first := o | |
| 91 | + else curModule^.last^.link := o; | |
| 92 | + curModule^.last := o; | |
| 93 | + end; | |
| 94 | +end; | |
| 95 | + | |
| 96 | +function ConstValue(var x: TItem): int64; | |
| 97 | +begin | |
| 98 | + if x.mode <> mConst then Mark('constant expected'); | |
| 99 | + ConstValue := x.val; | |
| 100 | +end; | |
| 101 | + | |
| 102 | +function ConstExpr: int64; | |
| 103 | +var x: TItem; | |
| 104 | +begin | |
| 105 | + Expression(x, nil); | |
| 106 | + if not IsInt(x.typ) then Mark('integer constant expected'); | |
| 107 | + ConstExpr := ConstValue(x); | |
| 108 | +end; | |
| 109 | + | |
| 110 | +function QualIdent: PObj; | |
| 111 | +var o: PObj; | |
| 112 | +begin | |
| 113 | + if tok <> sIdent then Mark('name expected'); | |
| 114 | + o := Lookup(id); | |
| 115 | + if o = nil then Mark(NameOf(id) + ' isn''t declared'); | |
| 116 | + Next; | |
| 117 | + if o^.cls = oModule then begin | |
| 118 | + Expect(sPeriod, '.'); | |
| 119 | + if tok <> sIdent then Mark('name expected'); | |
| 120 | + o := Find(o^.module^.scope, id); | |
| 121 | + if (o = nil) or not o^.exported then Mark(NameOf(id) + ' isn''t exported'); | |
| 122 | + Next; | |
| 123 | + end; | |
| 124 | + QualIdent := o; | |
| 125 | +end; | |
| 126 | + | |
| 127 | +function NewPtr(base: PType; vol: boolean): PType; | |
| 128 | +var t: PType; | |
| 129 | +begin | |
| 130 | + t := NewType(fPtr, 8, 8); | |
| 131 | + t^.base := base; | |
| 132 | + t^.vol := vol; | |
| 133 | + NewPtr := t; | |
| 134 | +end; | |
| 135 | + | |
| 136 | +{ Types } | |
| 137 | + | |
| 138 | +function RecordType: PType; | |
| 139 | +var | |
| 140 | + t, ft: PType; f, last: PObj; names: array[0..63] of longint; | |
| 141 | + exps: array[0..63] of boolean; n, i: longint; off, a, maxA: int64; | |
| 142 | +begin | |
| 143 | + t := NewType(fRecord, 0, 1); | |
| 144 | + if tok = sPacked then begin t^.packd := true; Next end; | |
| 145 | + Expect(sRecord, 'record'); | |
| 146 | + t^.scope := NewScope; | |
| 147 | + off := 0; maxA := 1; last := nil; | |
| 148 | + while tok = sIdent do begin | |
| 149 | + n := 0; | |
| 150 | + repeat | |
| 151 | + if n > High(names) then Mark('too many names'); | |
| 152 | + IdentDef(names[n], exps[n]); | |
| 153 | + inc(n); | |
| 154 | + if tok <> sComma then break; | |
| 155 | + Next; | |
| 156 | + until false; | |
| 157 | + Expect(sColon, ':'); | |
| 158 | + ft := ParseType; | |
| 159 | + if t^.packd then a := 1 else a := ft^.align; | |
| 160 | + for i := 0 to n - 1 do begin | |
| 161 | + off := AlignUp(off, a); | |
| 162 | + f := NewObj(oField, names[i], ft); | |
| 163 | + f^.exported := exps[i]; | |
| 164 | + f^.val := off; | |
| 165 | + Insert(f, t^.scope); | |
| 166 | + if last = nil then t^.fields := f else last^.next := f; | |
| 167 | + last := f; | |
| 168 | + off := off + ft^.size; | |
| 169 | + end; | |
| 170 | + if a > maxA then maxA := a; | |
| 171 | + Expect(sSemicolon, ';'); | |
| 172 | + end; | |
| 173 | + Expect(sEnd, 'end'); | |
| 174 | + t^.align := maxA; | |
| 175 | + t^.size := AlignUp(off, maxA); | |
| 176 | + RecordType := t; | |
| 177 | +end; | |
| 178 | + | |
| 179 | +function VariantType: PType; | |
| 180 | +var t, ft: PType; c, lastC, f, lastF: PObj; tag, off, maxSize: int64; n: longint; | |
| 181 | +begin | |
| 182 | + Expect(sVariant, 'variant'); | |
| 183 | + t := NewType(fVariant, 8, 8); | |
| 184 | + tag := 0; maxSize := 8; lastC := nil; | |
| 185 | + while tok = sIdent do begin | |
| 186 | + c := NewObj(oCase, id, t); | |
| 187 | + c^.val := tag; | |
| 188 | + c^.exported := declExported; | |
| 189 | + Declare(c); | |
| 190 | + if lastC = nil then t^.fields := c else lastC^.next := c; | |
| 191 | + lastC := c; | |
| 192 | + Next; | |
| 193 | + off := 8; | |
| 194 | + if tok = sLparen then begin | |
| 195 | + Next; | |
| 196 | + lastF := nil; | |
| 197 | + repeat | |
| 198 | + n := 0; | |
| 199 | + repeat | |
| 200 | + if tok <> sIdent then Mark('name expected'); | |
| 201 | + f := NewObj(oField, id, nil); | |
| 202 | + f^.exported := true; | |
| 203 | + if lastF = nil then c^.fields := f else lastF^.next := f; | |
| 204 | + lastF := f; | |
| 205 | + inc(n); | |
| 206 | + Next; | |
| 207 | + if tok <> sComma then break; | |
| 208 | + Next; | |
| 209 | + until false; | |
| 210 | + Expect(sColon, ':'); | |
| 211 | + ft := ParseType; | |
| 212 | + f := c^.fields; | |
| 213 | + while f^.typ <> nil do f := f^.next; | |
| 214 | + while f <> nil do begin | |
| 215 | + f^.typ := ft; | |
| 216 | + off := AlignUp(off, ft^.align); | |
| 217 | + f^.val := off; | |
| 218 | + off := off + ft^.size; | |
| 219 | + f := f^.next; | |
| 220 | + end; | |
| 221 | + if ft^.align > t^.align then t^.align := ft^.align; | |
| 222 | + if tok <> sSemicolon then break; | |
| 223 | + Next; | |
| 224 | + until false; | |
| 225 | + Expect(sRparen, ')'); | |
| 226 | + end; | |
| 227 | + if off > maxSize then maxSize := off; | |
| 228 | + Expect(sSemicolon, ';'); | |
| 229 | + inc(tag); | |
| 230 | + end; | |
| 231 | + Expect(sEnd, 'end'); | |
| 232 | + t^.len := tag; | |
| 233 | + t^.size := AlignUp(maxSize, t^.align); | |
| 234 | + VariantType := t; | |
| 235 | +end; | |
| 236 | + | |
| 237 | +function Signature: PType; | |
| 238 | +var | |
| 239 | + t, pt: PType; p, first, last: PObj; isVar: boolean; n: longint; | |
| 240 | +begin | |
| 241 | + t := NewType(fProc, 8, 8); | |
| 242 | + last := nil; n := 0; | |
| 243 | + if tok = sLparen then begin | |
| 244 | + Next; | |
| 245 | + if tok <> sRparen then | |
| 246 | + repeat | |
| 247 | + if tok = sEllipsis then begin | |
| 248 | + Next; t^.variadic := true; break | |
| 249 | + end; | |
| 250 | + isVar := tok = sVar; | |
| 251 | + if isVar then Next; | |
| 252 | + first := nil; | |
| 253 | + repeat | |
| 254 | + if tok <> sIdent then Mark('name expected'); | |
| 255 | + p := NewObj(oVar, id, nil); | |
| 256 | + p^.isVar := isVar; | |
| 257 | + p^.lev := 1; | |
| 258 | + if last = nil then t^.fields := p else last^.next := p; | |
| 259 | + last := p; | |
| 260 | + if first = nil then first := p; | |
| 261 | + inc(n); | |
| 262 | + Next; | |
| 263 | + if tok <> sComma then break; | |
| 264 | + Next; | |
| 265 | + until false; | |
| 266 | + Expect(sColon, ':'); | |
| 267 | + pt := ParseType; | |
| 268 | + while first <> nil do begin first^.typ := pt; first := first^.next end; | |
| 269 | + if tok <> sSemicolon then break; | |
| 270 | + Next; | |
| 271 | + until false; | |
| 272 | + Expect(sRparen, ')'); | |
| 273 | + end; | |
| 274 | + t^.len := n; | |
| 275 | + if tok = sColon then begin | |
| 276 | + Next; | |
| 277 | + if tok = sLparen then Mark('several results come in stage 2'); | |
| 278 | + t^.base := ParseType; | |
| 279 | + end; | |
| 280 | + Signature := t; | |
| 281 | +end; | |
| 282 | + | |
| 283 | +function ParseType: PType; | |
| 284 | +var t: PType; o: PObj; n: int64; vol: boolean; | |
| 285 | +begin | |
| 286 | + case tok of | |
| 287 | + sIdent: begin | |
| 288 | + o := QualIdent; | |
| 289 | + if o^.cls <> oType then Mark('type expected'); | |
| 290 | + t := o^.typ; | |
| 291 | + end; | |
| 292 | + sLbrak: begin | |
| 293 | + Next; | |
| 294 | + if tok = sRbrak then begin | |
| 295 | + Next; | |
| 296 | + t := NewType(fSlice, 16, 8); | |
| 297 | + t^.base := ParseType(); | |
| 298 | + end else begin | |
| 299 | + n := ConstExpr; | |
| 300 | + if (n <= 0) or (n > (int64(1) shl 40)) then Mark('bad array length'); | |
| 301 | + Expect(sRbrak, ']'); | |
| 302 | + t := NewType(fArray, 0, 1); | |
| 303 | + t^.len := n; | |
| 304 | + t^.base := ParseType(); | |
| 305 | + t^.size := n * t^.base^.size; | |
| 306 | + t^.align := t^.base^.align; | |
| 307 | + end; | |
| 308 | + end; | |
| 309 | + sArrow: begin | |
| 310 | + Next; | |
| 311 | + vol := tok = sVolatile; | |
| 312 | + if vol then Next; | |
| 313 | + if inTypes and (tok = sIdent) and (Lookup(id) = nil) then begin | |
| 314 | + { a type declared later in this section } | |
| 315 | + t := NewPtr(nil, vol); | |
| 316 | + t^.fwd := id; | |
| 317 | + if numFwd > High(fwdTypes) then Mark('too many forward pointers'); | |
| 318 | + fwdTypes[numFwd] := t; | |
| 319 | + inc(numFwd); | |
| 320 | + Next; | |
| 321 | + end else | |
| 322 | + t := NewPtr(ParseType(), vol); | |
| 323 | + end; | |
| 324 | + sPacked, sRecord: t := RecordType; | |
| 325 | + sVariant: t := VariantType; | |
| 326 | + sProc: begin Next; t := Signature end; | |
| 327 | + sLparen, sSet: Mark('enumerations and sets come in stage 2'); | |
| 328 | + else Mark('type expected'); | |
| 329 | + end; | |
| 330 | + ParseType := t; | |
| 331 | +end; | |
| 332 | + | |
| 333 | +{ Expressions } | |
| 334 | + | |
| 335 | +procedure Call(var x: TItem); | |
| 336 | +var c: TCall; a: TItem; want: PType; | |
| 337 | +begin | |
| 338 | + BeginCall(c, x); | |
| 339 | + Expect(sLparen, '('); | |
| 340 | + if tok <> sRparen then | |
| 341 | + repeat | |
| 342 | + if c.param <> nil then want := c.param^.typ else want := nil; | |
| 343 | + Expression(a, want); | |
| 344 | + PassArg(c, a); | |
| 345 | + if tok <> sComma then break; | |
| 346 | + Next; | |
| 347 | + until false; | |
| 348 | + Expect(sRparen, ')'); | |
| 349 | + EndCall(c, x); | |
| 350 | +end; | |
| 351 | + | |
| 352 | +procedure Args(n: longint; var slots: array of int64); | |
| 353 | +var i: longint; a: TItem; | |
| 354 | +begin | |
| 355 | + for i := 0 to n - 1 do begin | |
| 356 | + if i > 0 then Expect(sComma, ','); | |
| 357 | + Expression(a, nil); | |
| 358 | + if not (IsScalar(a.typ) and (a.typ^.form <> fReal)) then Mark('integer or pointer expected'); | |
| 359 | + if a.typ^.form = fConst then Coerce(a, tI64); | |
| 360 | + slots[i] := ToSlot(a); | |
| 361 | + end; | |
| 362 | +end; | |
| 363 | + | |
| 364 | +procedure Atomic(var x: TItem; which: longint); | |
| 365 | +var p, a: TItem; t: PType; s: array[0..2] of int64; i, n: longint; | |
| 366 | +begin | |
| 367 | + Expression(p, nil); | |
| 368 | + if p.typ^.form <> fPtr then Mark('a pointer expected'); | |
| 369 | + t := p.typ^.base; | |
| 370 | + if not (((t^.form = fInt) or ((t^.form = fPtr) and (which <> bXadd))) and (t^.size = 8)) then | |
| 371 | + Mark('an 8-byte integer or pointer expected'); | |
| 372 | + s[0] := ToSlot(p); | |
| 373 | + if which = bCas then n := 2 else n := 1; | |
| 374 | + for i := 1 to n do begin | |
| 375 | + Expect(sComma, ','); | |
| 376 | + Expression(a, t); | |
| 377 | + Coerce(a, t); | |
| 378 | + s[i] := ToSlot(a); | |
| 379 | + end; | |
| 380 | + Load(RCX, Mem(RBP, s[0]), 8, false); | |
| 381 | + Load(RAX, Mem(RBP, s[1]), 8, false); | |
| 382 | + MakeConst(x, t, 0); | |
| 383 | + case which of | |
| 384 | + bCas: begin | |
| 385 | + Load(RDX, Mem(RBP, s[2]), 8, false); | |
| 386 | + LockCmpxchg(Mem(RCX, 0), RDX); | |
| 387 | + SetCC(ccE, RAX); | |
| 388 | + x.typ := tBool; | |
| 389 | + end; | |
| 390 | + bXchg: Xchg(Mem(RCX, 0), RAX); | |
| 391 | + bXadd: LockXadd(Mem(RCX, 0), RAX); | |
| 392 | + end; | |
| 393 | + x.mode := mReg; | |
| 394 | +end; | |
| 395 | + | |
| 396 | +procedure Builtin(var x: TItem); | |
| 397 | +const SysRegs: array[0..6] of longint = (RAX, RDI, RSI, RDX, R10, R8, R9); | |
| 398 | +var which, n, i: longint; y: TItem; t: PType; slots: array[0..6] of int64; | |
| 399 | +begin | |
| 400 | + which := x.obj^.val; | |
| 401 | + if which in [bFence, bArgc, bArgv] then begin | |
| 402 | + if tok = sLparen then begin Next; Expect(sRparen, ')') end; | |
| 403 | + MakeConst(x, tNone, 0); | |
| 404 | + case which of | |
| 405 | + bFence: begin Mfence; x.mode := mNone end; | |
| 406 | + bArgc: begin Load(RAX, MemRip(argcChunk, 0), 8, false); x.typ := tI64; x.mode := mReg end; | |
| 407 | + bArgv: begin Load(RAX, MemRip(argvChunk, 0), 8, false); x.typ := tArgv; x.mode := mReg end; | |
| 408 | + end; | |
| 409 | + exit; | |
| 410 | + end; | |
| 411 | + Expect(sLparen, '('); | |
| 412 | + case which of | |
| 413 | + bSize: begin | |
| 414 | + t := ParseType; | |
| 415 | + MakeConst(x, tConst, t^.size); | |
| 416 | + end; | |
| 417 | + bAdr: begin | |
| 418 | + Expression(y, nil); | |
| 419 | + if (y.mode <> mVar) or not y.isVar then Mark('ADR needs a variable'); | |
| 420 | + t := NewPtr(y.typ, y.vol); | |
| 421 | + Address(y, RAX); | |
| 422 | + MakeConst(x, t, 0); | |
| 423 | + x.mode := mReg; | |
| 424 | + end; | |
| 425 | + bLen: begin | |
| 426 | + Expression(y, nil); | |
| 427 | + if y.mode = mStr then MakeConst(x, tConst, y.val) | |
| 428 | + else if y.typ^.form = fArray then MakeConst(x, tConst, y.typ^.len) | |
| 429 | + else if (y.typ^.form = fSlice) and (y.mode = mVar) then begin | |
| 430 | + Address(y, RAX); | |
| 431 | + Load(RAX, Mem(RAX, 8), 8, false); | |
| 432 | + MakeConst(x, tI64, 0); | |
| 433 | + x.mode := mReg; | |
| 434 | + end else Mark('LEN needs an array or slice'); | |
| 435 | + end; | |
| 436 | + bSyscall: begin | |
| 437 | + n := 1; | |
| 438 | + Args(1, slots); | |
| 439 | + while (tok = sComma) and (n < 7) do begin | |
| 440 | + Next; | |
| 441 | + Expression(y, nil); | |
| 442 | + if not (IsScalar(y.typ) and (y.typ^.form <> fReal)) then Mark('integer or pointer expected'); | |
| 443 | + if y.typ^.form = fConst then Coerce(y, tI64); | |
| 444 | + slots[n] := ToSlot(y); | |
| 445 | + inc(n); | |
| 446 | + end; | |
| 447 | + for i := 0 to n - 1 do Load(SysRegs[i], Mem(RBP, slots[i]), 8, false); | |
| 448 | + Syscall; | |
| 449 | + MakeConst(x, tI64, 0); | |
| 450 | + x.mode := mReg; | |
| 451 | + end; | |
| 452 | + bCas, bXchg, bXadd: Atomic(x, which); | |
| 453 | + bNew, bMsg: Mark('NEW and MSG come in stage 2'); | |
| 454 | + end; | |
| 455 | + Expect(sRparen, ')'); | |
| 456 | +end; | |
| 457 | + | |
| 458 | +procedure ConstructCase(var x: TItem; c: PObj); | |
| 459 | +var f: PObj; a: TItem; | |
| 460 | +begin | |
| 461 | + Construct(x, c); | |
| 462 | + f := c^.fields; | |
| 463 | + if tok = sLparen then begin | |
| 464 | + Next; | |
| 465 | + while f <> nil do begin | |
| 466 | + Expression(a, f^.typ); | |
| 467 | + SetField(x, f, a); | |
| 468 | + f := f^.next; | |
| 469 | + if f <> nil then Expect(sComma, ','); | |
| 470 | + end; | |
| 471 | + Expect(sRparen, ')'); | |
| 472 | + end else if f <> nil then Mark(NameOf(c^.name) + ' needs its fields'); | |
| 473 | +end; | |
| 474 | + | |
| 475 | +{ A designator, with any selectors and calls after it. called tells | |
| 476 | + whether the last thing done was a call. } | |
| 477 | +procedure Designator(var x: TItem; var called: boolean); | |
| 478 | +var o, f: PObj; y, z: TItem; t: PType; | |
| 479 | +begin | |
| 480 | + o := QualIdent; | |
| 481 | + called := false; | |
| 482 | + if o^.cls = oModule then Mark('a module is not a value'); | |
| 483 | + MakeItem(x, o); | |
| 484 | + if x.mode = mBuiltin then begin | |
| 485 | + Builtin(x); | |
| 486 | + called := true; | |
| 487 | + end else if x.mode = mCase then begin | |
| 488 | + ConstructCase(x, o); | |
| 489 | + end; | |
| 490 | + repeat | |
| 491 | + case tok of | |
| 492 | + sPeriod: begin | |
| 493 | + Next; | |
| 494 | + if (x.mode <> mVar) or (x.typ^.form <> fRecord) then Mark('not a record'); | |
| 495 | + if tok <> sIdent then Mark('field expected'); | |
| 496 | + f := Find(x.typ^.scope, id); | |
| 497 | + if f = nil then Mark('no field ' + NameOf(id)); | |
| 498 | + if not f^.exported and (x.typ^.module <> curModule) then | |
| 499 | + Mark('field ' + NameOf(id) + ' isn''t exported'); | |
| 500 | + Next; | |
| 501 | + Field(x, f); | |
| 502 | + called := false; | |
| 503 | + end; | |
| 504 | + sArrow: begin Next; Deref(x); called := false end; | |
| 505 | + sLbrak: begin | |
| 506 | + Next; | |
| 507 | + IndexPrep(x); | |
| 508 | + Expression(y, nil); | |
| 509 | + if tok = sUpto then begin | |
| 510 | + Next; | |
| 511 | + SliceLo(y); | |
| 512 | + Expression(z, nil); | |
| 513 | + Slice(x, y, z); | |
| 514 | + end else Index(x, y); | |
| 515 | + Expect(sRbrak, ']'); | |
| 516 | + called := false; | |
| 517 | + end; | |
| 518 | + sLparen: begin | |
| 519 | + if x.mode = mType then begin | |
| 520 | + t := x.typ; | |
| 521 | + Next; | |
| 522 | + Expression(x, nil); | |
| 523 | + Expect(sRparen, ')'); | |
| 524 | + Convert(x, t); | |
| 525 | + end else begin | |
| 526 | + Call(x); | |
| 527 | + called := true; | |
| 528 | + end; | |
| 529 | + end; | |
| 530 | + else break; | |
| 531 | + end; | |
| 532 | + until false; | |
| 533 | +end; | |
| 534 | + | |
| 535 | +procedure ArrayLiteral(var x: TItem; want: PType); | |
| 536 | +var | |
| 537 | + et, at: PType; e: TItem; slots: array[0..1023] of int64; n, i: longint; | |
| 538 | + off, es: int64; | |
| 539 | +begin | |
| 540 | + if (want = nil) or not (want^.form in [fArray, fSlice]) then | |
| 541 | + Mark('an array literal needs a known array or slice type'); | |
| 542 | + et := want^.base; | |
| 543 | + es := et^.size; | |
| 544 | + Next; | |
| 545 | + n := 0; | |
| 546 | + if tok <> sRbrak then | |
| 547 | + repeat | |
| 548 | + if n > High(slots) then Mark('too many elements'); | |
| 549 | + Expression(e, et); | |
| 550 | + Coerce(e, et); | |
| 551 | + if IsScalar(et) then slots[n] := ToSlot(e) | |
| 552 | + else begin | |
| 553 | + if e.isVar or e.vol or (e.mode <> mVar) or (e.base <> bFrame) then CopyToTemp(e); | |
| 554 | + slots[n] := e.off; | |
| 555 | + end; | |
| 556 | + inc(n); | |
| 557 | + if tok <> sComma then break; | |
| 558 | + Next; | |
| 559 | + until false; | |
| 560 | + Expect(sRbrak, ']'); | |
| 561 | + if (want^.form = fArray) and (n <> want^.len) then Mark('wrong number of elements'); | |
| 562 | + if n = 0 then begin | |
| 563 | + MovRI(RAX, 0); | |
| 564 | + SliceOf(x, want, 0); | |
| 565 | + exit; | |
| 566 | + end; | |
| 567 | + if want^.form = fArray then begin | |
| 568 | + at := want; | |
| 569 | + off := AllocTemp(at^.size, at^.align); | |
| 570 | + end else begin | |
| 571 | + at := NewType(fArray, n * es, et^.align); | |
| 572 | + at^.len := n; | |
| 573 | + at^.base := et; | |
| 574 | + off := AllocPerm(at^.size, at^.align); { lives as long as the call } | |
| 575 | + end; | |
| 576 | + for i := 0 to n - 1 do | |
| 577 | + if IsScalar(et) then begin | |
| 578 | + Load(RAX, Mem(RBP, slots[i]), 8, false); | |
| 579 | + Store(Mem(RBP, off + i * es), RAX, ScalarSize(et)); | |
| 580 | + end else begin | |
| 581 | + Lea(RSI, Mem(RBP, slots[i])); | |
| 582 | + Lea(RDI, Mem(RBP, off + i * es)); | |
| 583 | + MovRI(RCX, es); | |
| 584 | + RepMovsb; | |
| 585 | + end; | |
| 586 | + TempItem(x, at, off); | |
| 587 | +end; | |
| 588 | + | |
| 589 | +procedure Factor(var x: TItem; want: PType); | |
| 590 | +var called, vol: boolean; t: PType; | |
| 591 | +begin | |
| 592 | + case tok of | |
| 593 | + sInt, sChar: begin MakeConst(x, tConst, ival); Next end; | |
| 594 | + sReal: begin MakeReal(x, rval); Next end; | |
| 595 | + sString: begin MakeStr(x); Next end; | |
| 596 | + sIdent: begin | |
| 597 | + Designator(x, called); | |
| 598 | + if x.mode = mNone then Mark('this procedure returns nothing'); | |
| 599 | + end; | |
| 600 | + sLparen: begin Next; Expression(x, want); Expect(sRparen, ')') end; | |
| 601 | + sNot: begin Next; Factor(x, nil); NotOp(x) end; | |
| 602 | + sLbrak: ArrayLiteral(x, want); | |
| 603 | + sLbrace: Mark('record and set literals come in stage 2'); | |
| 604 | + sArrow: begin | |
| 605 | + Next; | |
| 606 | + vol := tok = sVolatile; | |
| 607 | + if vol then Next; | |
| 608 | + t := NewPtr(ParseType, vol); | |
| 609 | + Expect(sLparen, '('); | |
| 610 | + Expression(x, nil); | |
| 611 | + Expect(sRparen, ')'); | |
| 612 | + Convert(x, t); | |
| 613 | + end; | |
| 614 | + else Mark('expression expected'); | |
| 615 | + end; | |
| 616 | +end; | |
| 617 | + | |
| 618 | +procedure Term(var x: TItem; want: PType); | |
| 619 | +var op: TSym; y: TItem; L: longint; | |
| 620 | +begin | |
| 621 | + Factor(x, want); | |
| 622 | + while tok in [sTimes, sSlash, sDiv, sMod, sAnd, sShl, sShr] do begin | |
| 623 | + op := tok; | |
| 624 | + Next; | |
| 625 | + Op1(op, x, L); | |
| 626 | + Factor(y, nil); | |
| 627 | + Op2(op, x, y, L); | |
| 628 | + end; | |
| 629 | +end; | |
| 630 | + | |
| 631 | +procedure SimpleExpr(var x: TItem; want: PType); | |
| 632 | +var op: TSym; y: TItem; L: longint; | |
| 633 | +begin | |
| 634 | + if tok = sMinus then begin | |
| 635 | + Next; | |
| 636 | + Term(x, nil); | |
| 637 | + Neg(x); | |
| 638 | + end else Term(x, want); | |
| 639 | + while tok in [sPlus, sMinus, sOr, sXor] do begin | |
| 640 | + op := tok; | |
| 641 | + Next; | |
| 642 | + Op1(op, x, L); | |
| 643 | + Term(y, nil); | |
| 644 | + Op2(op, x, y, L); | |
| 645 | + end; | |
| 646 | +end; | |
| 647 | + | |
| 648 | +procedure Expression(var x: TItem; want: PType); | |
| 649 | +var op: TSym; y: TItem; L: longint; | |
| 650 | +begin | |
| 651 | + SimpleExpr(x, want); | |
| 652 | + if tok in [sEql, sNeq, sLss, sLeq, sGtr, sGeq] then begin | |
| 653 | + op := tok; | |
| 654 | + Next; | |
| 655 | + Op1(op, x, L); | |
| 656 | + SimpleExpr(y, nil); | |
| 657 | + Op2(op, x, y, L); | |
| 658 | + end else if tok = sIn then Mark('sets come in stage 2'); | |
| 659 | +end; | |
| 660 | + | |
| 661 | +{ Statements } | |
| 662 | + | |
| 663 | +procedure Loop(var brk: longint; var old: PLongint); | |
| 664 | +begin | |
| 665 | + brk := NoLabel; | |
| 666 | + old := curBreak; | |
| 667 | + curBreak := @brk; | |
| 668 | +end; | |
| 669 | + | |
| 670 | +procedure IfStatement; | |
| 671 | +var x: TItem; L, endL: longint; | |
| 672 | +begin | |
| 673 | + Next; | |
| 674 | + Expression(x, nil); | |
| 675 | + Expect(sThen, 'then'); | |
| 676 | + L := NoLabel; | |
| 677 | + CondJump(x, L); | |
| 678 | + Statements; | |
| 679 | + endL := NoLabel; | |
| 680 | + while tok = sElsif do begin | |
| 681 | + Jmp(endL); | |
| 682 | + FixLink(L); | |
| 683 | + Next; | |
| 684 | + Expression(x, nil); | |
| 685 | + Expect(sThen, 'then'); | |
| 686 | + L := NoLabel; | |
| 687 | + CondJump(x, L); | |
| 688 | + Statements; | |
| 689 | + end; | |
| 690 | + if tok = sElse then begin | |
| 691 | + Jmp(endL); | |
| 692 | + FixLink(L); | |
| 693 | + Next; | |
| 694 | + Statements; | |
| 695 | + end else FixLink(L); | |
| 696 | + Expect(sEnd, 'end'); | |
| 697 | + FixLink(endL); | |
| 698 | +end; | |
| 699 | + | |
| 700 | +procedure WhileStatement; | |
| 701 | +var x: TItem; top, L, brk: longint; old: PLongint; | |
| 702 | +begin | |
| 703 | + Next; | |
| 704 | + top := pc; | |
| 705 | + Expression(x, nil); | |
| 706 | + Expect(sDo, 'do'); | |
| 707 | + L := NoLabel; | |
| 708 | + CondJump(x, L); | |
| 709 | + Loop(brk, old); | |
| 710 | + Statements; | |
| 711 | + JmpBack(top); | |
| 712 | + Expect(sEnd, 'end'); | |
| 713 | + FixLink(L); | |
| 714 | + FixLink(brk); | |
| 715 | + curBreak := old; | |
| 716 | +end; | |
| 717 | + | |
| 718 | +procedure RepeatStatement; | |
| 719 | +var x: TItem; top, L, brk: longint; old: PLongint; | |
| 720 | +begin | |
| 721 | + Next; | |
| 722 | + top := pc; | |
| 723 | + Loop(brk, old); | |
| 724 | + Statements; | |
| 725 | + curBreak := old; | |
| 726 | + Expect(sUntil, 'until'); | |
| 727 | + Expression(x, nil); | |
| 728 | + L := NoLabel; | |
| 729 | + CondJump(x, L); | |
| 730 | + FixLinkTo(L, top); | |
| 731 | + FixLink(brk); | |
| 732 | +end; | |
| 733 | + | |
| 734 | +procedure ForStatement; | |
| 735 | +var | |
| 736 | + o: PObj; v, a, b, s: TItem; bslot, step: int64; top, exitL, brk: longint; | |
| 737 | + old: PLongint; | |
| 738 | +begin | |
| 739 | + Next; | |
| 740 | + if tok <> sIdent then Mark('name expected'); | |
| 741 | + o := Lookup(id); | |
| 742 | + if (o = nil) or (o^.cls <> oVar) or (o^.typ^.form <> fInt) then | |
| 743 | + Mark('a for loop needs an integer variable'); | |
| 744 | + Next; | |
| 745 | + Expect(sBecomes, ':='); | |
| 746 | + MakeItem(v, o); | |
| 747 | + Save(v); | |
| 748 | + Expression(a, o^.typ); | |
| 749 | + Assign(v, a); | |
| 750 | + Expect(sTo, 'to'); | |
| 751 | + Expression(b, o^.typ); | |
| 752 | + Coerce(b, o^.typ); | |
| 753 | + bslot := ToSlot(b); | |
| 754 | + step := 1; | |
| 755 | + if tok = sBy then begin | |
| 756 | + Next; | |
| 757 | + Expression(s, nil); | |
| 758 | + step := ConstValue(s); | |
| 759 | + if step = 0 then Mark('the step can''t be zero'); | |
| 760 | + end; | |
| 761 | + Expect(sDo, 'do'); | |
| 762 | + exitL := NoLabel; | |
| 763 | + top := pc; | |
| 764 | + MakeItem(v, o); | |
| 765 | + Value(v); | |
| 766 | + Load(RCX, Mem(RBP, bslot), 8, false); | |
| 767 | + Alu(aluCmp, RAX, RCX); | |
| 768 | + if o^.typ^.signed then begin | |
| 769 | + if step > 0 then Jcc(ccG, exitL) else Jcc(ccL, exitL) | |
| 770 | + end else begin | |
| 771 | + if step > 0 then Jcc(ccA, exitL) else Jcc(ccB, exitL) | |
| 772 | + end; | |
| 773 | + Loop(brk, old); | |
| 774 | + Statements; | |
| 775 | + curBreak := old; | |
| 776 | + Expect(sEnd, 'end'); | |
| 777 | + ForNext(o, bslot, step, exitL, top); | |
| 778 | + FixLink(exitL); | |
| 779 | + FixLink(brk); | |
| 780 | +end; | |
| 781 | + | |
| 782 | +procedure MatchStatement; | |
| 783 | +var | |
| 784 | + x: TItem; t: PType; c, f, o: PObj; p: int64; L, endL, n: longint; | |
| 785 | + seen: array[0..255] of boolean; hasElse: boolean; | |
| 786 | +begin | |
| 787 | + Next; | |
| 788 | + Expression(x, nil); | |
| 789 | + p := MatchBegin(x); | |
| 790 | + t := x.typ; | |
| 791 | + Expect(sOf, 'of'); | |
| 792 | + FillChar(seen, SizeOf(seen), 0); | |
| 793 | + hasElse := false; | |
| 794 | + endL := NoLabel; | |
| 795 | + while tok = sBar do begin | |
| 796 | + Next; | |
| 797 | + if hasElse then Mark('else must be the last arm'); | |
| 798 | + if tok = sElse then begin | |
| 799 | + Next; | |
| 800 | + Expect(sColon, ':'); | |
| 801 | + Statements; | |
| 802 | + hasElse := true; | |
| 803 | + end else begin | |
| 804 | + if tok <> sIdent then Mark('case expected'); | |
| 805 | + c := t^.fields; | |
| 806 | + while (c <> nil) and (c^.name <> id) do c := c^.next; | |
| 807 | + if c = nil then Mark(NameOf(id) + ' isn''t a case of ' + TypeName(t)); | |
| 808 | + if (c^.val <= High(seen)) and seen[c^.val] then Mark('case twice'); | |
| 809 | + if c^.val <= High(seen) then seen[c^.val] := true; | |
| 810 | + Next; | |
| 811 | + L := NoLabel; | |
| 812 | + MatchTag(p, c^.val, L); | |
| 813 | + OpenScope(NewScope); | |
| 814 | + if tok = sLparen then begin | |
| 815 | + Next; | |
| 816 | + f := c^.fields; | |
| 817 | + n := 0; | |
| 818 | + repeat | |
| 819 | + if tok <> sIdent then Mark('name expected'); | |
| 820 | + if f = nil then Mark('too many fields'); | |
| 821 | + o := NewObj(oVar, id, f^.typ); | |
| 822 | + o^.lev := 1; o^.ind := true; o^.isVar := true; | |
| 823 | + o^.val := p; o^.off := f^.val; | |
| 824 | + Insert(o, TopScope); | |
| 825 | + f := f^.next; | |
| 826 | + inc(n); | |
| 827 | + Next; | |
| 828 | + if tok <> sComma then break; | |
| 829 | + Next; | |
| 830 | + until false; | |
| 831 | + if f <> nil then Mark('name all the fields, or none'); | |
| 832 | + Expect(sRparen, ')'); | |
| 833 | + end; | |
| 834 | + Expect(sColon, ':'); | |
| 835 | + Statements; | |
| 836 | + CloseScope; | |
| 837 | + Jmp(endL); | |
| 838 | + FixLink(L); | |
| 839 | + end; | |
| 840 | + end; | |
| 841 | + Expect(sEnd, 'end'); | |
| 842 | + if not hasElse then begin | |
| 843 | + c := t^.fields; | |
| 844 | + while c <> nil do begin | |
| 845 | + if (c^.val > High(seen)) or not seen[c^.val] then Mark('no arm for ' + NameOf(c^.name)); | |
| 846 | + c := c^.next; | |
| 847 | + end; | |
| 848 | + end; | |
| 849 | + FixLink(endL); | |
| 850 | +end; | |
| 851 | + | |
| 852 | +procedure Statement; | |
| 853 | +var x, y: TItem; called: boolean; m: int64; c: TCall; | |
| 854 | +begin | |
| 855 | + m := TempMark; | |
| 856 | + case tok of | |
| 857 | + sIdent: begin | |
| 858 | + Designator(x, called); | |
| 859 | + if tok = sBecomes then begin | |
| 860 | + if (x.mode <> mVar) or not x.isVar then Mark('can''t assign to this'); | |
| 861 | + Save(x); | |
| 862 | + Next; | |
| 863 | + Expression(y, x.typ); | |
| 864 | + Assign(x, y); | |
| 865 | + end else if tok = sComma then | |
| 866 | + Mark('several results come in stage 2') | |
| 867 | + else if not called then begin | |
| 868 | + if (x.mode = mProc) or ((x.mode in [mVar, mReg]) and (x.typ^.form = fProc)) then begin | |
| 869 | + BeginCall(c, x); | |
| 870 | + EndCall(c, x); | |
| 871 | + end else Mark('not a statement'); | |
| 872 | + end; | |
| 873 | + end; | |
| 874 | + sIf: IfStatement; | |
| 875 | + sWhile: WhileStatement; | |
| 876 | + sRepeat: RepeatStatement; | |
| 877 | + sFor: ForStatement; | |
| 878 | + sMatch: MatchStatement; | |
| 879 | + sReturn: begin | |
| 880 | + Next; | |
| 881 | + if tok = sSemicolon then ReturnNone | |
| 882 | + else begin | |
| 883 | + Expression(x, curResult); | |
| 884 | + ReturnValue(x); | |
| 885 | + end; | |
| 886 | + end; | |
| 887 | + sBreak: begin | |
| 888 | + Next; | |
| 889 | + if curBreak = nil then Mark('break outside a loop'); | |
| 890 | + Jmp(curBreak^); | |
| 891 | + end; | |
| 892 | + sDefer: Mark('defer comes in stage 2'); | |
| 893 | + else Mark('statement expected'); | |
| 894 | + end; | |
| 895 | + ReleaseTemps(m); | |
| 896 | +end; | |
| 897 | + | |
| 898 | +procedure Statements; | |
| 899 | +begin | |
| 900 | + while not (tok in [sEnd, sElse, sElsif, sUntil, sBar, sEof]) do begin | |
| 901 | + Statement; | |
| 902 | + Expect(sSemicolon, ';'); | |
| 903 | + end; | |
| 904 | +end; | |
| 905 | + | |
| 906 | +{ Declarations } | |
| 907 | + | |
| 908 | +{ In a file read for its interface only: skip a procedure's body, from | |
| 909 | + its local declarations to the end that closes it. } | |
| 910 | +procedure SkipBody; | |
| 911 | +var d: longint; sawBegin: boolean; | |
| 912 | +begin | |
| 913 | + d := 0; | |
| 914 | + sawBegin := false; | |
| 915 | + repeat | |
| 916 | + case tok of | |
| 917 | + sBegin: begin if d = 0 then sawBegin := true; inc(d) end; | |
| 918 | + sIf, sWhile, sFor, sMatch, sRecord, sVariant: inc(d); | |
| 919 | + sEnd: begin | |
| 920 | + dec(d); | |
| 921 | + if (d = 0) and sawBegin then begin Next; exit end; | |
| 922 | + end; | |
| 923 | + sEof: Mark('end expected'); | |
| 924 | + end; | |
| 925 | + Next; | |
| 926 | + until false; | |
| 927 | +end; | |
| 928 | + | |
| 929 | +procedure Declarations(top: boolean); forward; | |
| 930 | + | |
| 931 | +procedure Body(p: PObj; sig: PType); | |
| 932 | +var q, params, last, o: PObj; old: PType; | |
| 933 | +begin | |
| 934 | + OpenScope(NewScope); | |
| 935 | + params := nil; last := nil; | |
| 936 | + q := sig^.fields; | |
| 937 | + while q <> nil do begin | |
| 938 | + o := NewObj(oVar, q^.name, q^.typ); | |
| 939 | + o^.lev := 1; | |
| 940 | + o^.isVar := q^.isVar; | |
| 941 | + if last = nil then params := o else last^.next := o; | |
| 942 | + last := o; | |
| 943 | + q := q^.next; | |
| 944 | + end; | |
| 945 | + BeginProc(sig, params); | |
| 946 | + o := params; | |
| 947 | + while o <> nil do begin | |
| 948 | + q := o^.next; | |
| 949 | + Insert(o, TopScope); | |
| 950 | + o := q; | |
| 951 | + end; | |
| 952 | + Declarations(false); | |
| 953 | + Expect(sBegin, 'begin'); | |
| 954 | + old := curResult; | |
| 955 | + curResult := sig^.base; | |
| 956 | + EnterBody(p^.chunk); | |
| 957 | + Statements; | |
| 958 | + Expect(sEnd, 'end'); | |
| 959 | + EndProc(p^.chunk); | |
| 960 | + curResult := old; | |
| 961 | + CloseScope; | |
| 962 | +end; | |
| 963 | + | |
| 964 | +procedure CodeBody(p: PObj; sig: PType); | |
| 965 | +var q: PObj; ni, nf: longint; v: int64; | |
| 966 | +begin | |
| 967 | + ni := 0; nf := 0; | |
| 968 | + q := sig^.fields; | |
| 969 | + while q <> nil do begin | |
| 970 | + if not q^.isVar and not IsScalar(q^.typ) then | |
| 971 | + Mark('a code procedure takes integers, BOOL, F64, pointers and procedures'); | |
| 972 | + if (q^.typ^.form = fReal) and not q^.isVar then inc(nf) else inc(ni); | |
| 973 | + q := q^.next; | |
| 974 | + end; | |
| 975 | + if (ni > 6) or (nf > 8) or sig^.variadic then Mark('too many parameters for a code procedure'); | |
| 976 | + if (sig^.base <> nil) and not IsScalar(sig^.base) then Mark('a code procedure returns a single value'); | |
| 977 | + BeginChunk(p^.chunk); | |
| 978 | + repeat | |
| 979 | + v := ConstExpr; | |
| 980 | + if (v < 0) or (v > 255) then Mark('a byte expected'); | |
| 981 | + Put1(v); | |
| 982 | + if tok <> sComma then break; | |
| 983 | + Next; | |
| 984 | + until false; | |
| 985 | + EndChunk(p^.chunk); | |
| 986 | +end; | |
| 987 | + | |
| 988 | +procedure ProcDecl; | |
| 989 | +var n: longint; exp: boolean; sig: PType; p: PObj; | |
| 990 | +begin | |
| 991 | + Next; | |
| 992 | + IdentDef(n, exp); | |
| 993 | + sig := Signature; | |
| 994 | + Expect(sSemicolon, ';'); | |
| 995 | + p := Find(TopScope, n); | |
| 996 | + if (p <> nil) and (p^.cls = oProc) and p^.fwd then begin | |
| 997 | + if not Same(p^.typ, sig) then Mark('not the signature it was declared with'); | |
| 998 | + if tok = sForward then Mark('declared forward twice'); | |
| 999 | + p^.exported := p^.exported or exp; | |
| 1000 | + end else begin | |
| 1001 | + p := NewObj(oProc, n, sig); | |
| 1002 | + p^.exported := exp; | |
| 1003 | + if (curModule^.name = nameRt) and not checkMode and (n = nameFail) then p^.chunk := rtFail | |
| 1004 | + else if (curModule^.name = nameRt) and not checkMode and (n = nameEq) then p^.chunk := rtEq | |
| 1005 | + else p^.chunk := NewChunk(secText, curModule^.name, n); | |
| 1006 | + Declare(p); | |
| 1007 | + end; | |
| 1008 | + if tok = sForward then begin | |
| 1009 | + Next; | |
| 1010 | + p^.fwd := true; | |
| 1011 | + exit; | |
| 1012 | + end; | |
| 1013 | + p^.fwd := false; | |
| 1014 | + if tok = sCode then begin | |
| 1015 | + Next; | |
| 1016 | + p^.isCode := true; | |
| 1017 | + CodeBody(p, sig); | |
| 1018 | + exit; | |
| 1019 | + end; | |
| 1020 | + if checkMode then SkipBody else Body(p, sig); | |
| 1021 | + if tok = sIdent then begin | |
| 1022 | + if id <> n then Mark('the procedure is ' + NameOf(n)); | |
| 1023 | + Next; | |
| 1024 | + end; | |
| 1025 | +end; | |
| 1026 | + | |
| 1027 | +procedure Declarations(top: boolean); | |
| 1028 | +var | |
| 1029 | + n, i, k: longint; exp: boolean; x: TItem; o: PObj; t: PType; | |
| 1030 | + names: array[0..63] of longint; exps: array[0..63] of boolean; | |
| 1031 | +begin | |
| 1032 | + repeat | |
| 1033 | + case tok of | |
| 1034 | + sConst: begin | |
| 1035 | + Next; | |
| 1036 | + while tok = sIdent do begin | |
| 1037 | + IdentDef(n, exp); | |
| 1038 | + if tok = sColon then Mark('errors come in stage 2'); | |
| 1039 | + Expect(sEql, '='); | |
| 1040 | + Expression(x, nil); | |
| 1041 | + o := NewObj(oConst, n, x.typ); | |
| 1042 | + o^.exported := exp and top; | |
| 1043 | + if x.mode = mStr then begin o^.val := x.val; o^.chunk := x.chunk end | |
| 1044 | + else o^.val := ConstValue(x); | |
| 1045 | + Declare(o); | |
| 1046 | + Expect(sSemicolon, ';'); | |
| 1047 | + end; | |
| 1048 | + end; | |
| 1049 | + sType: begin | |
| 1050 | + Next; | |
| 1051 | + inTypes := true; | |
| 1052 | + numFwd := 0; | |
| 1053 | + while tok = sIdent do begin | |
| 1054 | + IdentDef(n, exp); | |
| 1055 | + Expect(sEql, '='); | |
| 1056 | + declExported := exp and top; | |
| 1057 | + t := ParseType; | |
| 1058 | + o := NewObj(oType, n, t); | |
| 1059 | + o^.exported := exp and top; | |
| 1060 | + if t^.obj = nil then t^.obj := o; | |
| 1061 | + Declare(o); | |
| 1062 | + Expect(sSemicolon, ';'); | |
| 1063 | + end; | |
| 1064 | + for i := 0 to numFwd - 1 do begin | |
| 1065 | + o := Lookup(fwdTypes[i]^.fwd); | |
| 1066 | + if (o = nil) or (o^.cls <> oType) then Mark(NameOf(fwdTypes[i]^.fwd) + ' isn''t declared'); | |
| 1067 | + fwdTypes[i]^.base := o^.typ; | |
| 1068 | + fwdTypes[i]^.fwd := -1; | |
| 1069 | + end; | |
| 1070 | + inTypes := false; | |
| 1071 | + declExported := false; | |
| 1072 | + end; | |
| 1073 | + sVar: begin | |
| 1074 | + Next; | |
| 1075 | + while tok = sIdent do begin | |
| 1076 | + k := 0; | |
| 1077 | + repeat | |
| 1078 | + if k > High(names) then Mark('too many names'); | |
| 1079 | + IdentDef(names[k], exps[k]); | |
| 1080 | + inc(k); | |
| 1081 | + if tok <> sComma then break; | |
| 1082 | + Next; | |
| 1083 | + until false; | |
| 1084 | + Expect(sColon, ':'); | |
| 1085 | + declExported := top and exps[0]; | |
| 1086 | + t := ParseType; | |
| 1087 | + declExported := false; | |
| 1088 | + for i := 0 to k - 1 do begin | |
| 1089 | + o := NewObj(oVar, names[i], t); | |
| 1090 | + o^.exported := exps[i] and top; | |
| 1091 | + o^.isVar := false; | |
| 1092 | + if top then begin | |
| 1093 | + o^.lev := 0; | |
| 1094 | + o^.chunk := NewChunk(secBss, curModule^.name, names[i]); | |
| 1095 | + SetBss(o^.chunk, t^.size, t^.align); | |
| 1096 | + end else begin | |
| 1097 | + o^.lev := 1; | |
| 1098 | + o^.val := AllocPerm(t^.size, t^.align); | |
| 1099 | + end; | |
| 1100 | + Declare(o); | |
| 1101 | + end; | |
| 1102 | + Expect(sSemicolon, ';'); | |
| 1103 | + end; | |
| 1104 | + end; | |
| 1105 | + sProc: begin | |
| 1106 | + if not top then Mark('procedures don''t nest'); | |
| 1107 | + ProcDecl; | |
| 1108 | + Expect(sSemicolon, ';'); | |
| 1109 | + end; | |
| 1110 | + sExtern: Mark('extern comes in stage 3'); | |
| 1111 | + else exit; | |
| 1112 | + end; | |
| 1113 | + until false; | |
| 1114 | +end; | |
| 1115 | + | |
| 1116 | +{ Modules } | |
| 1117 | + | |
| 1118 | +function FindModule(n: longint): PModule; | |
| 1119 | +var m: PModule; | |
| 1120 | +begin | |
| 1121 | + m := firstModule; | |
| 1122 | + while (m <> nil) and (m^.name <> n) do m := m^.next; | |
| 1123 | + FindModule := m; | |
| 1124 | +end; | |
| 1125 | + | |
| 1126 | +{ Modules being compiled, not yet finished: for finding a cycle. } | |
| 1127 | +var | |
| 1128 | + pending: array[0..63] of PModule; | |
| 1129 | + numPending: longint; | |
| 1130 | + | |
| 1131 | +function Exists(const path: shortstring): boolean; | |
| 1132 | +var st: Stat; p: shortstring; | |
| 1133 | +begin | |
| 1134 | + p := path + #0; | |
| 1135 | + Exists := (FpStat(@p[1], st) = 0) and ((st.st_mode and S_IFMT) = S_IFREG); | |
| 1136 | +end; | |
| 1137 | + | |
| 1138 | +function CompileFile(const path: shortstring; n: longint; checkOnly: boolean): PModule; | |
| 1139 | +var | |
| 1140 | + st: TScanState; saved: array[0..MaxDepth - 1] of longint; d: longint; | |
| 1141 | + oldMod: PModule; oldFile: longint; oldCheck: boolean; m, im: PModule; | |
| 1142 | + imp: PImport; alias, mname: longint; o: PObj; body: longint; | |
| 1143 | +begin | |
| 1144 | + SaveScan(st); | |
| 1145 | + SaveScopes(d, saved); | |
| 1146 | + oldMod := curModule; oldFile := fileChunk; oldCheck := checkMode; | |
| 1147 | + if not Open(path) then Fatal('can''t read ' + path); | |
| 1148 | + New(m); | |
| 1149 | + FillChar(m^, SizeOf(TModule), 0); | |
| 1150 | + m^.name := n; | |
| 1151 | + m^.scope := NewScope; | |
| 1152 | + m^.path := path; | |
| 1153 | + m^.body := -1; | |
| 1154 | + m^.checkOnly := checkOnly; | |
| 1155 | + curModule := m; | |
| 1156 | + fileChunk := -1; | |
| 1157 | + checkMode := checkOnly; | |
| 1158 | + if not checkOnly then begin | |
| 1159 | + if numPending > High(pending) then Mark('modules nested too deeply'); | |
| 1160 | + pending[numPending] := m; | |
| 1161 | + inc(numPending); | |
| 1162 | + end; | |
| 1163 | + OpenScope(m^.scope); | |
| 1164 | + Next; | |
| 1165 | + Expect(sModule, 'module'); | |
| 1166 | + if (tok <> sIdent) or (id <> n) then Mark('this file must hold module ' + NameOf(n)); | |
| 1167 | + Next; | |
| 1168 | + if tok = sLparen then Mark('generic modules come in stage 2'); | |
| 1169 | + Expect(sSemicolon, ';'); | |
| 1170 | + while tok = sImport do begin | |
| 1171 | + Next; | |
| 1172 | + repeat | |
| 1173 | + if tok <> sIdent then Mark('module name expected'); | |
| 1174 | + alias := id; | |
| 1175 | + mname := id; | |
| 1176 | + Next; | |
| 1177 | + if tok = sBecomes then begin | |
| 1178 | + Next; | |
| 1179 | + if tok <> sIdent then Mark('module name expected'); | |
| 1180 | + mname := id; | |
| 1181 | + Next; | |
| 1182 | + if tok = sLparen then Mark('generic modules come in stage 2'); | |
| 1183 | + end; | |
| 1184 | + im := ImportModule(mname); | |
| 1185 | + o := NewObj(oModule, alias, nil); | |
| 1186 | + o^.module := im; | |
| 1187 | + Declare(o); | |
| 1188 | + New(imp); | |
| 1189 | + imp^.module := im; | |
| 1190 | + imp^.next := m^.imports; | |
| 1191 | + m^.imports := imp; | |
| 1192 | + if tok <> sComma then break; | |
| 1193 | + Next; | |
| 1194 | + until false; | |
| 1195 | + Expect(sSemicolon, ';'); | |
| 1196 | + end; | |
| 1197 | + if tok = sLink then Mark('link comes in stage 3'); | |
| 1198 | + Declarations(true); | |
| 1199 | + if tok = sBegin then begin | |
| 1200 | + if checkMode then SkipBody | |
| 1201 | + else begin | |
| 1202 | + Next; | |
| 1203 | + body := NewChunk(secText, n, nameBody); | |
| 1204 | + m^.body := body; | |
| 1205 | + BeginProc(tBodySig, nil); | |
| 1206 | + curResult := nil; | |
| 1207 | + EnterBody(body); | |
| 1208 | + Statements; | |
| 1209 | + Expect(sEnd, 'end'); | |
| 1210 | + EndProc(body); | |
| 1211 | + end; | |
| 1212 | + end else Expect(sEnd, 'end'); | |
| 1213 | + if (tok <> sIdent) or (id <> n) then Mark('end ' + NameOf(n) + ' expected'); | |
| 1214 | + Next; | |
| 1215 | + if tok <> sPeriod then Mark('. expected'); | |
| 1216 | + o := m^.first; | |
| 1217 | + while o <> nil do begin | |
| 1218 | + if (o^.cls = oProc) and o^.fwd then Mark(NameOf(o^.name) + ' is declared forward but never defined'); | |
| 1219 | + o := o^.link; | |
| 1220 | + end; | |
| 1221 | + CloseScope; | |
| 1222 | + m^.done := true; | |
| 1223 | + if not checkOnly then begin | |
| 1224 | + dec(numPending); | |
| 1225 | + if lastModule = nil then firstModule := m else lastModule^.next := m; | |
| 1226 | + lastModule := m; | |
| 1227 | + end; | |
| 1228 | + curModule := oldMod; fileChunk := oldFile; checkMode := oldCheck; | |
| 1229 | + RestoreScopes(d, saved); | |
| 1230 | + RestoreScan(st); | |
| 1231 | + CompileFile := m; | |
| 1232 | +end; | |
| 1233 | + | |
| 1234 | +{ The interface check: b must export just what a does. } | |
| 1235 | +procedure CompareIface(a, b: PModule); | |
| 1236 | +var o, q: PObj; | |
| 1237 | +begin | |
| 1238 | + o := a^.first; | |
| 1239 | + while o <> nil do begin | |
| 1240 | + if o^.exported then begin | |
| 1241 | + q := Find(b^.scope, o^.name); | |
| 1242 | + if (q = nil) or not q^.exported or (Fingerprint(o) <> Fingerprint(q)) then | |
| 1243 | + Fatal(b^.path + ': ' + NameOf(o^.name) + ' isn''t exported as in ' + a^.path); | |
| 1244 | + end; | |
| 1245 | + o := o^.link; | |
| 1246 | + end; | |
| 1247 | + q := b^.first; | |
| 1248 | + while q <> nil do begin | |
| 1249 | + o := Find(a^.scope, q^.name); | |
| 1250 | + if q^.exported and ((o = nil) or not o^.exported) then | |
| 1251 | + Fatal(b^.path + ': ' + NameOf(q^.name) + ' isn''t exported by ' + a^.path); | |
| 1252 | + q := q^.link; | |
| 1253 | + end; | |
| 1254 | +end; | |
| 1255 | + | |
| 1256 | +{ Reads every other file of module n in dir, and checks its interface | |
| 1257 | + against m, compiled from the file named chosen. } | |
| 1258 | +procedure CheckSiblings(const dir, chosen: shortstring; n: longint; m: PModule); | |
| 1259 | +var | |
| 1260 | + dp: pDir; e: pDirent; f, prefix: shortstring; i: longint; | |
| 1261 | + others: array[0..31] of shortstring; numOthers: longint; | |
| 1262 | +begin | |
| 1263 | + prefix := NameOf(n) + '.'; | |
| 1264 | + numOthers := 0; | |
| 1265 | + dp := FpOpenDir(dir); | |
| 1266 | + if dp = nil then exit; | |
| 1267 | + repeat | |
| 1268 | + e := FpReadDir(dp^); | |
| 1269 | + if e = nil then break; | |
| 1270 | + f := ''; | |
| 1271 | + i := 0; | |
| 1272 | + while (e^.d_name[i] <> #0) and (i < 255) do begin | |
| 1273 | + f := f + e^.d_name[i]; | |
| 1274 | + inc(i); | |
| 1275 | + end; | |
| 1276 | + if (copy(f, 1, length(prefix)) = prefix) and (length(f) > 4) | |
| 1277 | + and (copy(f, length(f) - 3, 4) = '.jed') and (f <> chosen) | |
| 1278 | + and (numOthers <= High(others)) then begin | |
| 1279 | + others[numOthers] := f; | |
| 1280 | + inc(numOthers); | |
| 1281 | + end; | |
| 1282 | + until false; | |
| 1283 | + FpCloseDir(dp^); | |
| 1284 | + for i := 0 to numOthers - 1 do | |
| 1285 | + CompareIface(m, CompileFile(dir + '/' + others[i], n, true)); | |
| 1286 | +end; | |
| 1287 | + | |
| 1288 | +function ImportModule(name: longint): PModule; | |
| 1289 | +const Suffixes: array[0..3] of string[16] = ('.' + Target, '.' + TargetOS, '.' + TargetArch, ''); | |
| 1290 | +var m: PModule; i, j: longint; dir, f: shortstring; | |
| 1291 | +begin | |
| 1292 | + m := FindModule(name); | |
| 1293 | + if m <> nil then exit(m); | |
| 1294 | + for i := 0 to numPending - 1 do | |
| 1295 | + if pending[i]^.name = name then Mark(NameOf(name) + ' imports itself'); | |
| 1296 | + for i := -1 to numInc do begin | |
| 1297 | + if i = -1 then dir := mainDir | |
| 1298 | + else if i = numInc then dir := libDir | |
| 1299 | + else dir := incDirs[i]; | |
| 1300 | + for j := 0 to 3 do begin | |
| 1301 | + f := NameOf(name) + Suffixes[j] + '.jed'; | |
| 1302 | + if Exists(dir + '/' + f) then begin | |
| 1303 | + m := CompileFile(dir + '/' + f, name, false); | |
| 1304 | + CheckSiblings(dir, f, name, m); | |
| 1305 | + exit(m); | |
| 1306 | + end; | |
| 1307 | + end; | |
| 1308 | + end; | |
| 1309 | + Mark('can''t find module ' + NameOf(name)); | |
| 1310 | + ImportModule := nil; | |
| 1311 | +end; | |
| 1312 | + | |
| 1313 | +function CompileMain(const path: shortstring): PModule; | |
| 1314 | +var base: shortstring; i, n: longint; | |
| 1315 | +begin | |
| 1316 | + i := length(path); | |
| 1317 | + while (i > 0) and (path[i] <> '/') do dec(i); | |
| 1318 | + if i = 0 then mainDir := '.' else mainDir := copy(path, 1, i - 1); | |
| 1319 | + base := copy(path, i + 1, 255); | |
| 1320 | + i := pos('.', base); | |
| 1321 | + if i > 0 then base := copy(base, 1, i - 1); | |
| 1322 | + if base = '' then Fatal('no module in ' + path); | |
| 1323 | + n := Name(base); | |
| 1324 | + if n <> nameRt then ImportModule(nameRt); | |
| 1325 | + if FindModule(n) <> nil then CompileMain := FindModule(n) | |
| 1326 | + else CompileMain := CompileFile(path, n, false); | |
| 1327 | +end; | |
| 1328 | + | |
| 1329 | +initialization | |
| 1330 | + nameRt := Name('Rt'); | |
| 1331 | + nameFail := Name('fail'); | |
| 1332 | + nameEq := Name('eq'); | |
| 1333 | + nameMain := Name('main'); | |
| 1334 | + nameBody := Name('body'); | |
| 1335 | + rtFail := NewChunk(secText, nameRt, nameFail); | |
| 1336 | + rtEq := NewChunk(secText, nameRt, nameEq); | |
| 1337 | + argcChunk := NewChunk(secBss, -1, Name('argc')); | |
| 1338 | + SetBss(argcChunk, 8, 8); | |
| 1339 | + argvChunk := NewChunk(secBss, -1, Name('argv')); | |
| 1340 | + SetBss(argvChunk, 8, 8); | |
| 1341 | + tArgv := NewPtr(NewPtr(tU8, false), false); | |
| 1342 | + tBodySig := NewType(fProc, 8, 8); | |
| 1343 | + curBreak := nil; | |
| 1344 | + numPending := 0; | |
| 1345 | + firstModule := nil; | |
| 1346 | + lastModule := nil; | |
| 1347 | +end. |
| @@ -39,7 +39,7 @@ | ||
| 39 | 39 | buf: PChar; |
| 40 | 40 | len, pos, line, lineStart: longint; |
| 41 | 41 | tokPos, tokLine, tokLineStart: longint; |
| 42 | - sym: TSym; | |
| 42 | + tok: TSym; | |
| 43 | 43 | ival: int64; |
| 44 | 44 | rval: double; |
| 45 | 45 | id: longint; |
| @@ -51,7 +51,7 @@ | ||
| 51 | 51 | MaxStr = 65536; |
| 52 | 52 | |
| 53 | 53 | var |
| 54 | - sym: TSym; { the current symbol } | |
| 54 | + tok: TSym; { the current symbol } | |
| 55 | 55 | ival: int64; { sInt, sChar: its value } |
| 56 | 56 | rval: double; { sReal: its value } |
| 57 | 57 | id: longint; { sIdent: its name } |
| @@ -64,8 +64,8 @@ | ||
| 64 | 64 | procedure Next; |
| 65 | 65 | procedure Mark(const msg: shortstring); |
| 66 | 66 | procedure Fatal(const msg: shortstring); |
| 67 | -procedure Save(var st: TScanState); | |
| 68 | -procedure Restore(const st: TScanState); | |
| 67 | +procedure SaveScan(var st: TScanState); | |
| 68 | +procedure RestoreScan(const st: TScanState); | |
| 69 | 69 | function Where: shortstring; |
| 70 | 70 | function CurPath: shortstring; |
| 71 | 71 | function CurLine: longint; |
| @@ -84,7 +84,7 @@ | ||
| 84 | 84 | 'to', 'type', 'until', 'var', 'variant', 'VOLATILE', 'while', 'xor'); |
| 85 | 85 | |
| 86 | 86 | var |
| 87 | - cur: TScanState; { buf .. path; sym, ival, rval and id live above } | |
| 87 | + cur: TScanState; { buf .. path; tok, ival, rval and id live above } | |
| 88 | 88 | |
| 89 | 89 | { The name table: every distinct identifier once, in a pool of |
| 90 | 90 | characters, found through an open-addressed hash table. } |
| @@ -232,16 +232,16 @@ | ||
| 232 | 232 | Open := true; |
| 233 | 233 | end; |
| 234 | 234 | |
| 235 | -procedure Save(var st: TScanState); | |
| 235 | +procedure SaveScan(var st: TScanState); | |
| 236 | 236 | begin |
| 237 | 237 | st := cur; |
| 238 | - st.sym := sym; st.ival := ival; st.rval := rval; st.id := id; | |
| 238 | + st.tok := tok; st.ival := ival; st.rval := rval; st.id := id; | |
| 239 | 239 | end; |
| 240 | 240 | |
| 241 | -procedure Restore(const st: TScanState); | |
| 241 | +procedure RestoreScan(const st: TScanState); | |
| 242 | 242 | begin |
| 243 | 243 | cur := st; |
| 244 | - sym := st.sym; ival := st.ival; rval := st.rval; id := st.id; | |
| 244 | + tok := st.tok; ival := st.ival; rval := st.rval; id := st.id; | |
| 245 | 245 | end; |
| 246 | 246 | |
| 247 | 247 | { UTF-8 } |
| @@ -400,7 +400,7 @@ | ||
| 400 | 400 | v := v * 16 + qword(HexVal(cur.buf[i])); |
| 401 | 401 | end; |
| 402 | 402 | inc(cur.pos); |
| 403 | - sym := sInt; | |
| 403 | + tok := sInt; | |
| 404 | 404 | ival := int64(v); { 16 digits are the bits of a 64-bit word } |
| 405 | 405 | end else if (cur.buf[cur.pos] = '.') and (cur.buf[cur.pos + 1] in ['0'..'9']) then begin |
| 406 | 406 | if not allDec then Mark('bad number'); |
| @@ -417,7 +417,7 @@ | ||
| 417 | 417 | Move(cur.buf[start], s[1], cur.pos - start); |
| 418 | 418 | Val(s, rval, code); |
| 419 | 419 | if code <> 0 then Mark('bad real'); |
| 420 | - sym := sReal; | |
| 420 | + tok := sReal; | |
| 421 | 421 | end else begin |
| 422 | 422 | if not allDec then Mark('hex number needs H'); |
| 423 | 423 | v := 0; |
| @@ -426,7 +426,7 @@ | ||
| 426 | 426 | if v > (qword(high(int64)) - qword(d)) div 10 then Mark('number too big'); |
| 427 | 427 | v := v * 10 + qword(d); |
| 428 | 428 | end; |
| 429 | - sym := sInt; | |
| 429 | + tok := sInt; | |
| 430 | 430 | ival := int64(v); |
| 431 | 431 | end; |
| 432 | 432 | if cur.buf[cur.pos] in ['A'..'Z', 'a'..'z', '_', '0'..'9'] then Mark('bad number'); |
| @@ -458,8 +458,8 @@ | ||
| 458 | 458 | until not (cur.buf[cur.pos] in ['A'..'Z', 'a'..'z', '0'..'9', '_']); |
| 459 | 459 | if cur.pos - start > MaxIdent then Mark('name too long'); |
| 460 | 460 | h := Intern(@cur.buf[start], cur.pos - start); |
| 461 | - if h < NumWords then sym := TSym(ord(sAnd) + h) | |
| 462 | - else begin sym := sIdent; id := h end; | |
| 461 | + if h < NumWords then tok := TSym(ord(sAnd) + h) | |
| 462 | + else begin tok := sIdent; id := h end; | |
| 463 | 463 | end; |
| 464 | 464 | '0'..'9': Number; |
| 465 | 465 | '''': begin |
| @@ -468,7 +468,7 @@ | ||
| 468 | 468 | v := LitChar(false); |
| 469 | 469 | if cur.buf[cur.pos] <> '''' then Mark('character not closed'); |
| 470 | 470 | inc(cur.pos); |
| 471 | - sym := sChar; | |
| 471 | + tok := sChar; | |
| 472 | 472 | ival := v; |
| 473 | 473 | end; |
| 474 | 474 | '"': begin |
| @@ -480,50 +480,50 @@ | ||
| 480 | 480 | end; |
| 481 | 481 | inc(cur.pos); |
| 482 | 482 | sbuf[slen] := 0; |
| 483 | - sym := sString; | |
| 483 | + tok := sString; | |
| 484 | 484 | end; |
| 485 | - '+': begin inc(cur.pos); sym := sPlus end; | |
| 486 | - '-': begin inc(cur.pos); sym := sMinus end; | |
| 487 | - '*': begin inc(cur.pos); sym := sTimes end; | |
| 488 | - '/': begin inc(cur.pos); sym := sSlash end; | |
| 489 | - '=': begin inc(cur.pos); sym := sEql end; | |
| 490 | - '#': begin inc(cur.pos); sym := sNeq end; | |
| 485 | + '+': begin inc(cur.pos); tok := sPlus end; | |
| 486 | + '-': begin inc(cur.pos); tok := sMinus end; | |
| 487 | + '*': begin inc(cur.pos); tok := sTimes end; | |
| 488 | + '/': begin inc(cur.pos); tok := sSlash end; | |
| 489 | + '=': begin inc(cur.pos); tok := sEql end; | |
| 490 | + '#': begin inc(cur.pos); tok := sNeq end; | |
| 491 | 491 | '<': begin |
| 492 | 492 | inc(cur.pos); |
| 493 | - if cur.buf[cur.pos] = '=' then begin inc(cur.pos); sym := sLeq end | |
| 494 | - else sym := sLss; | |
| 493 | + if cur.buf[cur.pos] = '=' then begin inc(cur.pos); tok := sLeq end | |
| 494 | + else tok := sLss; | |
| 495 | 495 | end; |
| 496 | 496 | '>': begin |
| 497 | 497 | inc(cur.pos); |
| 498 | - if cur.buf[cur.pos] = '=' then begin inc(cur.pos); sym := sGeq end | |
| 499 | - else sym := sGtr; | |
| 498 | + if cur.buf[cur.pos] = '=' then begin inc(cur.pos); tok := sGeq end | |
| 499 | + else tok := sGtr; | |
| 500 | 500 | end; |
| 501 | 501 | ':': begin |
| 502 | 502 | inc(cur.pos); |
| 503 | - if cur.buf[cur.pos] = '=' then begin inc(cur.pos); sym := sBecomes end | |
| 504 | - else sym := sColon; | |
| 503 | + if cur.buf[cur.pos] = '=' then begin inc(cur.pos); tok := sBecomes end | |
| 504 | + else tok := sColon; | |
| 505 | 505 | end; |
| 506 | - ';': begin inc(cur.pos); sym := sSemicolon end; | |
| 507 | - ',': begin inc(cur.pos); sym := sComma end; | |
| 506 | + ';': begin inc(cur.pos); tok := sSemicolon end; | |
| 507 | + ',': begin inc(cur.pos); tok := sComma end; | |
| 508 | 508 | '.': begin |
| 509 | 509 | inc(cur.pos); |
| 510 | 510 | if cur.buf[cur.pos] = '.' then begin |
| 511 | 511 | inc(cur.pos); |
| 512 | - if cur.buf[cur.pos] = '.' then begin inc(cur.pos); sym := sEllipsis end | |
| 513 | - else sym := sUpto; | |
| 514 | - end else sym := sPeriod; | |
| 512 | + if cur.buf[cur.pos] = '.' then begin inc(cur.pos); tok := sEllipsis end | |
| 513 | + else tok := sUpto; | |
| 514 | + end else tok := sPeriod; | |
| 515 | 515 | end; |
| 516 | - '(': begin inc(cur.pos); sym := sLparen end; | |
| 517 | - ')': begin inc(cur.pos); sym := sRparen end; | |
| 518 | - '[': begin inc(cur.pos); sym := sLbrak end; | |
| 519 | - ']': begin inc(cur.pos); sym := sRbrak end; | |
| 520 | - '{': begin inc(cur.pos); sym := sLbrace end; | |
| 521 | - '}': begin inc(cur.pos); sym := sRbrace end; | |
| 522 | - '^': begin inc(cur.pos); sym := sArrow end; | |
| 523 | - '|': begin inc(cur.pos); sym := sBar end; | |
| 516 | + '(': begin inc(cur.pos); tok := sLparen end; | |
| 517 | + ')': begin inc(cur.pos); tok := sRparen end; | |
| 518 | + '[': begin inc(cur.pos); tok := sLbrak end; | |
| 519 | + ']': begin inc(cur.pos); tok := sRbrak end; | |
| 520 | + '{': begin inc(cur.pos); tok := sLbrace end; | |
| 521 | + '}': begin inc(cur.pos); tok := sRbrace end; | |
| 522 | + '^': begin inc(cur.pos); tok := sArrow end; | |
| 523 | + '|': begin inc(cur.pos); tok := sBar end; | |
| 524 | 524 | #0: begin |
| 525 | 525 | if cur.pos < cur.len then Mark('NUL character'); |
| 526 | - sym := sEof; | |
| 526 | + tok := sEof; | |
| 527 | 527 | end; |
| 528 | 528 | else |
| 529 | 529 | if byte(c) >= $80 then |
| @@ -66,6 +66,7 @@ | ||
| 66 | 66 | isCode: boolean; { oProc: a code procedure } |
| 67 | 67 | fields: PObj; { oCase: its fields } |
| 68 | 68 | module: PModule; { oModule: the module; others: where declared } |
| 69 | + link: PObj; { the next top-level declaration of its module } | |
| 69 | 70 | end; |
| 70 | 71 | |
| 71 | 72 | TImport = record |
| @@ -82,6 +83,7 @@ | ||
| 82 | 83 | body: longint; { the chunk of its body, or -1 } |
| 83 | 84 | done: boolean; { compiled to the end } |
| 84 | 85 | checkOnly: boolean; { read only to check another file's interface } |
| 86 | + used: boolean; { imported, through others, by the main module } | |
| 85 | 87 | next: PModule; { modules in the order they were finished } |
| 86 | 88 | end; |
| 87 | 89 | |
| @@ -89,6 +91,8 @@ | ||
| 89 | 91 | { built-in procedures } |
| 90 | 92 | bSize = 0; bAdr = 1; bLen = 2; bNew = 3; bMsg = 4; bSyscall = 5; |
| 91 | 93 | bFence = 6; bCas = 7; bXchg = 8; bXadd = 9; bArgc = 10; bArgv = 11; |
| 94 | + | |
| 95 | + MaxDepth = 64; { scopes open at once } | |
| 92 | 96 | |
| 93 | 97 | var |
| 94 | 98 | tI8, tI16, tI32, tI64, tU8, tU16, tU32, tU64: PType; |
| @@ -101,6 +105,8 @@ | ||
| 101 | 105 | procedure OpenScope(s: longint); |
| 102 | 106 | procedure CloseScope; |
| 103 | 107 | function TopScope: longint; |
| 108 | +procedure SaveScopes(var d: longint; var saved: array of longint); | |
| 109 | +procedure RestoreScopes(d: longint; const saved: array of longint); | |
| 104 | 110 | procedure Insert(obj: PObj; s: longint); |
| 105 | 111 | function Find(s, name: longint): PObj; |
| 106 | 112 | function Lookup(name: longint): PObj; |
| @@ -112,14 +118,10 @@ | ||
| 112 | 118 | function IsAggregate(t: PType): boolean; |
| 113 | 119 | function Fits(v: int64; t: PType): boolean; |
| 114 | 120 | function TypeName(t: PType): shortstring; |
| 115 | -function SameAcross(a, b: PType; ma, mb: PModule): boolean; | |
| 116 | -function SameSig(a, b: PType; ma, mb: PModule): boolean; | |
| 121 | +function Fingerprint(o: PObj): qword; | |
| 117 | 122 | |
| 118 | 123 | implementation |
| 119 | 124 | |
| 120 | -const | |
| 121 | - MaxDepth = 64; | |
| 122 | - | |
| 123 | 125 | type |
| 124 | 126 | PObjTab = ^TObjTab; |
| 125 | 127 | TObjTab = array[0..MaxInt div 16] of PObj; |
| @@ -177,6 +179,23 @@ | ||
| 177 | 179 | dec(depth); |
| 178 | 180 | end; |
| 179 | 181 | |
| 182 | +{ An imported module is compiled in the middle of another, and must | |
| 183 | + see only the universe and itself. } | |
| 184 | +procedure SaveScopes(var d: longint; var saved: array of longint); | |
| 185 | +var i: longint; | |
| 186 | +begin | |
| 187 | + d := depth; | |
| 188 | + for i := 0 to depth - 1 do saved[i] := scopes[i]; | |
| 189 | + depth := 1; | |
| 190 | +end; | |
| 191 | + | |
| 192 | +procedure RestoreScopes(d: longint; const saved: array of longint); | |
| 193 | +var i: longint; | |
| 194 | +begin | |
| 195 | + depth := d; | |
| 196 | + for i := 0 to depth - 1 do scopes[i] := saved[i]; | |
| 197 | +end; | |
| 198 | + | |
| 180 | 199 | function TopScope: longint; |
| 181 | 200 | begin |
| 182 | 201 | TopScope := scopes[depth - 1]; |
| @@ -343,50 +362,52 @@ | ||
| 343 | 362 | end; |
| 344 | 363 | end; |
| 345 | 364 | |
| 346 | -{ The interface check: types of two files of one module, where a type | |
| 347 | - declared in one file stands for the type of the same name in the | |
| 348 | - other. } | |
| 349 | -function SameAcross(a, b: PType; ma, mb: PModule): boolean; | |
| 350 | -var p, q: PObj; | |
| 365 | +{ A fingerprint of what a declaration exports, for the interface | |
| 366 | + check: two files of one module export the same thing when their | |
| 367 | + fingerprints agree. A named type counts by its name, so that a type | |
| 368 | + declared in each file stands for the other's. } | |
| 369 | +procedure Mix(var h: qword; v: int64); | |
| 351 | 370 | begin |
| 352 | - if a = b then exit(true); | |
| 353 | - if (a = nil) or (b = nil) then exit(false); | |
| 354 | - if (a^.obj <> nil) or (b^.obj <> nil) then begin | |
| 355 | - if (a^.obj = nil) or (b^.obj = nil) then exit(false); | |
| 356 | - exit((a^.obj^.module = ma) and (b^.obj^.module = mb) | |
| 357 | - and (a^.obj^.name = b^.obj^.name)); | |
| 371 | + h := (h xor qword(v)) * 1099511628211; | |
| 372 | +end; | |
| 373 | + | |
| 374 | +procedure Shape(var h: qword; t: PType; top: boolean); | |
| 375 | +var f, g: PObj; | |
| 376 | +begin | |
| 377 | + if t = nil then begin Mix(h, -1); exit end; | |
| 378 | + if (t^.obj <> nil) and not top then begin | |
| 379 | + Mix(h, t^.obj^.name); | |
| 380 | + if t^.obj^.module <> nil then Mix(h, t^.obj^.module^.name); | |
| 381 | + exit; | |
| 358 | 382 | end; |
| 359 | - if a^.form <> b^.form then exit(false); | |
| 360 | - case a^.form of | |
| 361 | - fPtr: SameAcross := (a^.vol = b^.vol) and SameAcross(a^.base, b^.base, ma, mb); | |
| 362 | - fArray: SameAcross := (a^.len = b^.len) and SameAcross(a^.base, b^.base, ma, mb); | |
| 363 | - fSlice: SameAcross := SameAcross(a^.base, b^.base, ma, mb); | |
| 364 | - fProc: SameAcross := SameSig(a, b, ma, mb); | |
| 365 | - fRecord, fVariant: begin | |
| 366 | - { anonymous: every field, exported or not, in order } | |
| 367 | - p := a^.fields; q := b^.fields; | |
| 368 | - while (p <> nil) and (q <> nil) do begin | |
| 369 | - if (p^.name <> q^.name) or not SameAcross(p^.typ, q^.typ, ma, mb) then exit(false); | |
| 370 | - p := p^.next; q := q^.next; | |
| 371 | - end; | |
| 372 | - SameAcross := (p = nil) and (q = nil); | |
| 383 | + Mix(h, ord(t^.form)); | |
| 384 | + Mix(h, ord(t^.vol) + 2 * ord(t^.variadic)); | |
| 385 | + Mix(h, t^.len); | |
| 386 | + if t^.form in [fPtr, fArray, fSlice, fProc] then Shape(h, t^.base, false); | |
| 387 | + f := t^.fields; | |
| 388 | + while f <> nil do begin | |
| 389 | + if (t^.form <> fRecord) or f^.exported then begin | |
| 390 | + Mix(h, f^.name); | |
| 391 | + Mix(h, ord(f^.isVar)); | |
| 392 | + Shape(h, f^.typ, false); | |
| 393 | + g := f^.fields; | |
| 394 | + while g <> nil do begin Shape(h, g^.typ, false); g := g^.next end; | |
| 373 | 395 | end; |
| 374 | - else SameAcross := false; | |
| 396 | + f := f^.next; | |
| 375 | 397 | end; |
| 376 | 398 | end; |
| 377 | 399 | |
| 378 | -function SameSig(a, b: PType; ma, mb: PModule): boolean; | |
| 379 | -var p, q: PObj; | |
| 400 | +function Fingerprint(o: PObj): qword; | |
| 401 | +var h: qword; f: PObj; | |
| 380 | 402 | begin |
| 381 | - if (a^.len <> b^.len) or (a^.variadic <> b^.variadic) then exit(false); | |
| 382 | - if (a^.base = nil) <> (b^.base = nil) then exit(false); | |
| 383 | - if (a^.base <> nil) and not SameAcross(a^.base, b^.base, ma, mb) then exit(false); | |
| 384 | - p := a^.fields; q := b^.fields; | |
| 385 | - while p <> nil do begin | |
| 386 | - if (p^.isVar <> q^.isVar) or not SameAcross(p^.typ, q^.typ, ma, mb) then exit(false); | |
| 387 | - p := p^.next; q := q^.next; | |
| 388 | - end; | |
| 389 | - SameSig := true; | |
| 403 | + h := 14695981039346656037; | |
| 404 | + Mix(h, ord(o^.cls)); | |
| 405 | + if o^.cls = oCase then begin | |
| 406 | + Mix(h, o^.val); | |
| 407 | + f := o^.fields; | |
| 408 | + while f <> nil do begin Shape(h, f^.typ, false); f := f^.next end; | |
| 409 | + end else Shape(h, o^.typ, o^.cls = oType); | |
| 410 | + Fingerprint := h; | |
| 390 | 411 | end; |
| 391 | 412 | |
| 392 | 413 | { The universe } |
| @@ -7,13 +7,13 @@ | ||
| 7 | 7 | if not Open(ParamStr(1)) then Fatal('no file'); |
| 8 | 8 | repeat |
| 9 | 9 | Next; |
| 10 | - write(Where, ' ', ord(sym)); | |
| 11 | - case sym of | |
| 10 | + write(Where, ' ', ord(tok)); | |
| 11 | + case tok of | |
| 12 | 12 | sIdent: write(' ', NameOf(id)); |
| 13 | 13 | sInt, sChar: write(' ', ival); |
| 14 | 14 | sReal: write(' ', rval:0:6); |
| 15 | 15 | sString: begin write(' "'); for i := 0 to slen - 1 do write(sbuf[i], ','); write('"') end; |
| 16 | 16 | end; |
| 17 | 17 | writeln; |
| 18 | - until sym = sEof; | |
| 18 | + until tok = sEof; | |
| 19 | 19 | end. |