unit labelsvars; interface type TGlobal = record Name: String; Size: Integer; Vtype: Byte; Places: Array of Longword; end; TLocal = record Name: String; Vtype: Byte; end; TGlobalArray = record Name: String; Items: Byte; Size: Integer; Vtypes: Byte; Places: Array of Longword; end; TLabel = record Name: String; Raw: Longword; Places: Array of Longword; end; TRawCode = Array of Byte; function GetGlobalIndex(Name: String): Integer; function GetLocalIndex(Name: String): Integer; function GetGlobalArrayIndex(Name: String): Integer; function GetLocalArrayIndex(Name: String): Integer; function GetLabelIndex(Name: String): Integer; procedure SetGlobal(s: String; pos: Integer); procedure SetGlobalArray(s: String; pos: Integer); procedure SetLabel(s: String; pos: Integer); function IsGlobal(s: String): Byte; function IsLocal(s: String): Byte; function IsGlobalArray(s: String): Byte; function IsLocalArray(s: String): Byte; function IsLabel(s: String): Boolean; procedure AddGlobal(s: String; Vtype: Char); procedure AddLocal(s: String; Vtype: Char); procedure AddGlobalArray(Name: String; Items: Integer; Vals: Char); procedure AddLocalArray(Name: String; Items: Integer; Vals: Char); procedure AddLabel(s: String; raw: Longword); procedure WriteGlobals(var rawcode: TRawCode); procedure WriteGlobalsArrays(var rawcode: TRawCode); procedure WriteLabels(var rawcode: TRawCode); var Globals: Array of TGlobal; Locals: Array of TLocal; GlobalArrays: Array of TGlobalArray; LocalArrays: Array of TGlobalArray; Labels: Array of TLabel; Highest: Integer = 0; ArrayHigh: Integer = 0; implementation function GetGlobalIndex(Name: String): Integer; begin Result:= 0; while (Result < Length(Globals)) and (Globals[Result].Name <> Name) do Inc(Result); end; function GetLocalIndex(Name: String): Integer; begin Result:= 0; while (Result < Length(Locals)) and (Locals[Result].Name <> Name) do Inc(Result); end; function GetGlobalArrayIndex(Name: String): Integer; begin Result:= 0; while (Result < Length(GlobalArrays)) and (GlobalArrays[Result].Name <> Name) do Inc(Result); end; function GetLocalArrayIndex(Name: String): Integer; begin Result:= 0; while (Result < Length(LocalArrays)) and (LocalArrays[Result].Name <> Name) do Inc(Result); end; function GetLabelIndex(Name: String): Integer; begin Result:= 0; while (Result < Length(Labels)) and (Labels[Result].Name <> Name) do Inc(Result); if (Result >= Length(Labels)) then begin AddLabel(Name,$00000000); Result:= Length(Labels)-1; end; end; procedure SetGlobal(s: String; pos: Integer); var x,i: Integer; begin i:= GetGlobalIndex(s); x:= Length(Globals[i].Places); SetLength(Globals[i].Places,x+1); Globals[i].Places[x]:= pos; end; procedure SetGlobalArray(s: String; pos: Integer); var x,i: Integer; begin i:= GetGlobalIndex(s); x:= Length(GlobalArrays[i].Places); SetLength(GlobalArrays[i].Places,x+1); GlobalArrays[i].Places[x]:= pos; end; procedure SetLabel(s: String; pos: Integer); var x,i: Integer; begin i:= GetLabelIndex(s); x:= Length(Labels[i].Places); SetLength(Labels[i].Places,x+1); Labels[i].Places[x]:= pos; end; function IsGlobal(s: String): Byte; var x: Integer; begin Result:= $FF; x:= 0; while (x < Length(Globals)) and (Result = $FF) do begin if (Globals[x].Name = s) then Result:= Globals[x].Vtype; Inc(x); end; end; function IsLocal(s: String): Byte; var x: Integer; begin Result:= $FF; x:= 0; while (x < Length(Locals)) and (Result = $FF) do begin if (Locals[x].Name = s) then Result:= Locals[x].Vtype; Inc(x); end; end; function IsGlobalArray(s: String): Byte; var x: Integer; begin Result:= $FF; x:= 0; while (x < Length(GlobalArrays)) and (Result = $FF) do begin if (GlobalArrays[x].Name = s) then Result:= GlobalArrays[x].Vtypes; Inc(x); end; end; function IsLocalArray(s: String): Byte; var x: Integer; begin Result:= $FF; x:= 0; while (x < Length(LocalArrays)) and (Result = $FF) do begin if (LocalArrays[x].Name = s) then Result:= LocalArrays[x].Vtypes; Inc(x); end; end; function IsLabel(s: String): Boolean; var x: Integer; begin Result:= False; x:= 0; while (x < Length(Labels)) do begin if (Labels[x].Name = s) then Result:= True; Inc(x); end; end; procedure AddGlobal(s: String; Vtype: Char); var x: Integer; begin x:= Length(Globals); SetLength(Globals,x+1); Globals[x].Name:= s; case Vtype of 'i': begin Globals[x].Vtype:= $00; Globals[x].Size:= 4; end; 'f': begin Globals[x].Vtype:= $01; Globals[x].Size:= 4; end; 'n': begin Globals[x].Vtype:= $02; Globals[x].Size:= 8; end; 's': begin Globals[x].Vtype:= $03; Globals[x].Size:= 16; end; end; end; procedure AddLocal(s: String; Vtype: Char); var x: Integer; begin if (IsLocal(s) <> $FF) then x:= GetLocalIndex(s) else begin x:= Length(Locals); SetLength(Locals,x+1); Locals[x].Name:= s; end; case Vtype of 'i': Locals[x].Vtype:= $00; 'f': Locals[x].Vtype:= $01; 'n': Locals[x].Vtype:= $02; 's': Locals[x].Vtype:= $03; end; end; procedure AddGlobalArray(Name: String; Items: Integer; Vals: Char); var x: Integer; begin x:= Length(GlobalArrays); SetLength(GlobalArrays,x+1); GlobalArrays[x].Name:= Name; GlobalArrays[x].Items:= Items; case Vals of 'i': begin GlobalArrays[x].Vtypes:= $00; GlobalArrays[x].Size:= Items*4; end; 'f': begin GlobalArrays[x].Vtypes:= $01; GlobalArrays[x].Size:= Items*4; end; 'n': begin GlobalArrays[x].Vtypes:= $02; GlobalArrays[x].Size:= Items*8; end; 's': begin GlobalArrays[x].Vtypes:= $03; GlobalArrays[x].Size:= Items*16; end; end; end; procedure AddLocalArray(Name: String; Items: Integer; Vals: Char); var x: Integer; begin x:= Length(LocalArrays); SetLength(LocalArrays,x+1); LocalArrays[x].Name:= Name; LocalArrays[x].Items:= Items; case Vals of 'i': begin LocalArrays[x].Vtypes:= $00; LocalArrays[x].Size:= Items*4; end; 'f': begin LocalArrays[x].Vtypes:= $01; LocalArrays[x].Size:= Items*4; end; 'n': begin LocalArrays[x].Vtypes:= $02; LocalArrays[x].Size:= Items*8; end; 's': begin LocalArrays[x].Vtypes:= $03; LocalArrays[x].Size:= Items*16; end; end; end; procedure AddLabel(s: String; raw: Longword); var x: Integer; begin if IsLabel(s) then Labels[GetLabelIndex(s)].Raw:= raw; x:= Length(Labels); SetLength(Labels,x+1); Labels[x].Name:= s; Labels[x].Raw:= raw; end; procedure WriteGlobals(var rawcode: TRawCode); var x,y: Integer; tmp: Word; begin x:= 0; tmp:= 4 + ArrayHigh + Highest; while (x < Length(Globals)) do begin y:= 0; while (y < Length(Globals[x].Places)) do begin Move(tmp,rawcode[Globals[x].Places[y]],2); Inc(y); end; tmp:= tmp + Globals[x].Size; Inc(x); end; end; procedure WriteGlobalsArrays(var rawcode: TRawCode); var x,y: Integer; tmp: Word; begin x:= 0; tmp:= 4 + Highest; while (x < Length(GlobalArrays)) do begin y:= 0; while (y < Length(GlobalArrays[x].Places)) do begin Move(tmp,rawcode[GlobalArrays[x].Places[y]],2); Move(GlobalArrays[x].Items,rawcode[GlobalArrays[x].Places[y]+4],1); Inc(y); end; tmp:= tmp + GlobalArrays[x].Size; ArrayHigh:= ArrayHigh + GlobalArrays[x].Size; Inc(x); end; end; procedure WriteLabels(var rawcode: TRawCode); var x,y: Integer; begin x:= 0; while (x < Length(Labels)) do begin y:= 0; while (y < Length(Labels[x].Places)) do begin Move(Labels[x].Raw,rawcode[Labels[x].Places[y]],2); Inc(y); end; Inc(x); end; end; end.