rautils.pas 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807
  1. {
  2. $Id$
  3. Copyright (c) 1998 Carl Eric Codere
  4. This unit implements some support routines for assembler parsing
  5. independent of the processor
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. **********************************************************************}
  18. Unit RAUtils;
  19. {*************************************************************************}
  20. { This unit implements some objects as well as utilities which will be }
  21. { used by all inline assembler parsers (non-processor specific). }
  22. { }
  23. { Main routines/objects herein: }
  24. { o Object TExprParse is a simple expression parser to resolve assembler }
  25. { expressions. (Based generally on some code by Thai Tran from SWAG). }
  26. { o Object TInstruction is a simple object used for instructions }
  27. { o Record TOperand is a simple record used to store information on }
  28. { each operand. }
  29. { o String conversion routines from octal,binary and hex to decimal. }
  30. { o A linked list object/record for local labels }
  31. { o Routines for retrieving symbols (local and global) }
  32. { o Object for a linked list of strings (with duplicate strings not }
  33. { allowed). }
  34. { o Non-processor dependant routines for adding instructions to the }
  35. { instruction list. }
  36. {*************************************************************************}
  37. {--------------------------------------------------------------------}
  38. { LEFT TO DO: }
  39. { o Fix the remaining bugs in the expression parser, such as with }
  40. { 4+-3 }
  41. { o Add support for local typed constants search. }
  42. { o Add support for private/protected fields in method assembler }
  43. { routines. }
  44. {--------------------------------------------------------------------}
  45. Interface
  46. Uses
  47. globtype,systems,
  48. symtable,aasm,hcodegen,verbose,globals,files,strings,
  49. cobjects,
  50. {$ifdef i386}
  51. {$ifdef Ag386Bin}
  52. i386base;
  53. {$else}
  54. i386;
  55. {$endif}
  56. {$endif}
  57. {$ifdef m68k}
  58. m68k;
  59. {$endif}
  60. Const
  61. RPNMax = 10; { I think you only need 4, but just to be safe }
  62. OpMax = 25;
  63. maxoperands = 3; { Maximum operands for assembler instructions }
  64. Type
  65. {---------------------------------------------------------------------}
  66. { Label Management types }
  67. {---------------------------------------------------------------------}
  68. PAsmLabel = ^TAsmLabel;
  69. { Each local label has this structure associated with it }
  70. TAsmLabel = record
  71. name: PString; { pointer to a pascal string name of label }
  72. lab: PLabel; { pointer to a label as defined in FPC }
  73. emitted: boolean; { as the label itself been emitted ? }
  74. next: PAsmLabel; { next node }
  75. end;
  76. TAsmLabelList = Object
  77. public
  78. First: PAsmLabel;
  79. Constructor Init;
  80. Destructor Done;
  81. Procedure Insert(s:string; lab: PLabel; emitted: boolean);
  82. Function Search(const s: string): PAsmLabel;
  83. private
  84. Last: PAsmLabel;
  85. Function NewPasStr(s:string): PString;
  86. end;
  87. {---------------------------------------------------------------------}
  88. { Instruction management types }
  89. {---------------------------------------------------------------------}
  90. toperandtype = (OPR_NONE,OPR_REFERENCE,OPR_CONSTANT,OPR_REGISTER,OPR_LABINSTR,
  91. OPR_REGLIST,OPR_SYMBOL);
  92. { When the TReference field isintvalue = TRUE }
  93. { then offset points to an ABSOLUTE address }
  94. { otherwise isintvalue should always be false }
  95. { Special cases: }
  96. { For the M68k Target, size is UNUSED, the }
  97. { opcode determines the size of the }
  98. { instruction. }
  99. { DIVS/DIVU/MULS/MULU of the form dn,dn:dn }
  100. { is stored as three operands!! }
  101. { Each instruction operand can be of this type }
  102. TOperand = record
  103. size: topsize;
  104. opinfo: longint; { ao_xxxx flags }
  105. overriden : boolean; { indicates if the opcode has been overriden }
  106. { by a pseudo-opcode such as DWORD PTR }
  107. case operandtype:toperandtype of
  108. { the size of the opr_none field should be at least equal to each }
  109. { other field as to facilitate initialization. }
  110. OPR_NONE: (l: array[1..sizeof(treference)] of byte);
  111. OPR_REFERENCE: (ref:treference);
  112. OPR_CONSTANT: (val: longint);
  113. OPR_REGISTER: (reg:tregister);
  114. OPR_LABINSTR: (hl: plabel);
  115. { Register list such as in the movem instruction }
  116. OPR_REGLIST: (list: set of tregister);
  117. OPR_SYMBOL : (symbol:pasmsymbol);
  118. end;
  119. TInstruction = object
  120. public
  121. operands: array[1..maxoperands] of TOperand;
  122. { if numops = zero, a size may still be valid in operands[1] }
  123. { it still should be checked. }
  124. numops: byte;
  125. { set to TRUE if the instruction is labeled. }
  126. labeled: boolean;
  127. { This is used for instructions such A_CMPSB... etc, to determine }
  128. { the size of the instruction. }
  129. stropsize: topsize;
  130. procedure init;
  131. procedure done;
  132. { sets up the prefix field with the instruction pointed to in s }
  133. procedure addprefix(tok: tasmop);
  134. { sets up the instruction with the instruction pointed to in s }
  135. procedure addinstr(tok: tasmop);
  136. { get the current instruction of this object }
  137. function getinstruction: tasmop;
  138. { get the current prefix of this instruction }
  139. function getprefix: tasmop;
  140. private
  141. prefix: tasmop;
  142. instruction: tasmop;
  143. end;
  144. {---------------------------------------------------------------------}
  145. { Expression parser types }
  146. {---------------------------------------------------------------------}
  147. { expression parser error codes }
  148. texpr_error =
  149. (zero_divide, { divide by zero. }
  150. stack_overflow, { stack overflow. }
  151. stack_underflow, { stack underflow. }
  152. invalid_number, { invalid conversion }
  153. invalid_op); { invalid operator }
  154. TExprOperator = record
  155. ch: char; { operator }
  156. is_prefix: boolean; { was it a prefix, possible prefixes are +,- and not }
  157. end;
  158. String15 = String[15];
  159. {**********************************************************************}
  160. { The following operators are supported: }
  161. { '+' : addition }
  162. { '-' : subtraction }
  163. { '*' : multiplication }
  164. { '/' : modulo division }
  165. { '^' : exclusive or }
  166. { '<' : shift left }
  167. { '>' : shift right }
  168. { '&' : bitwise and }
  169. { '|' : bitwise or }
  170. { '~' : bitwise complement }
  171. { '%' : modulo division }
  172. { nnn: longint numbers }
  173. { ( and ) parenthesis }
  174. {**********************************************************************}
  175. TExprParse = Object
  176. public
  177. Constructor Init;
  178. Destructor Done;
  179. Function Evaluate(Expr: String): longint;
  180. Procedure Error(anerror: texpr_error); virtual;
  181. Function Priority(_Operator: Char): Integer; virtual;
  182. private
  183. RPNStack : Array[1..RPNMax] of longint; { Stack For RPN calculator }
  184. RPNTop : Integer;
  185. OpStack : Array[1..OpMax] of TExprOperator; { Operator stack For conversion }
  186. OpTop : Integer;
  187. Procedure RPNPush(Num: Longint);
  188. Function RPNPop: Longint;
  189. Procedure RPNCalc(token: String15; prefix: boolean);
  190. Procedure OpPush(_Operator: char; prefix: boolean);
  191. { In reality returns TExprOperaotr }
  192. Procedure OpPop(var _Operator:TExprOperator);
  193. end;
  194. { Evaluate an expression string to a longint }
  195. Function CalculateExpression(const expression: string): longint;
  196. {---------------------------------------------------------------------}
  197. { String routines }
  198. {---------------------------------------------------------------------}
  199. {*********************************************************************}
  200. { PROCEDURE PadZero; }
  201. { Description: Makes sure that the string specified is of the given }
  202. { length, by padding it with binary zeros, or truncating if necessary}
  203. { Remark: The return value is determined BEFORE any eventual padding.}
  204. { Return Value: TRUE = if length of string s was <= then n }
  205. { FALSE = if length of string s was > then n }
  206. {*********************************************************************}
  207. Function PadZero(Var s: String; n: byte): Boolean;
  208. { Converts an Hex digit string to a Decimal string }
  209. { Returns '' if there was an error. }
  210. Function HexToDec(const S:String): String;
  211. { Converts a binary digit string to a Decimal string }
  212. { Returns '' if there was an error. }
  213. Function BinaryToDec(const S:String): String;
  214. { Converts an octal digit string to a Decimal string }
  215. { Returns '' if there was an error. }
  216. Function OctalToDec(const S:String): String;
  217. { Converts a string containing C styled escape sequences to }
  218. { a pascal style string. }
  219. Function EscapeToPascal(const s:string): string;
  220. Procedure ConcatPasString(p : paasmoutput;s:string);
  221. { Writes the string s directly to the assembler output }
  222. Procedure ConcatDirect(p : paasmoutput;s:string);
  223. {---------------------------------------------------------------------}
  224. { Symbol helper routines }
  225. {---------------------------------------------------------------------}
  226. Procedure SetOperandSize(var instr:TInstruction;operandnum,size:longint);
  227. Function GetVarOffsetSize(const base,field:string;Var Offset: longint;var Size:longint):boolean;
  228. Function GetTypeOffsetSize(const base,field: string;Var Offset: longint;var Size:longint):boolean;
  229. Function SearchIConstant(const s:string; var l:longint): boolean;
  230. Function SearchLabel(const s: string; var hl: plabel): boolean;
  231. Function CreateVarInstr(var Instr: TInstruction; const hs:string;
  232. operandnum:byte):boolean;
  233. {*********************************************************************}
  234. { FUNCTION NewPasStr(s:string): PString }
  235. { Description: This routine allocates a string on the heap and }
  236. { returns a pointer to the allocated string. }
  237. { }
  238. { Remarks: The string allocated should not be modified, since it's }
  239. { length will be less then 255. }
  240. { Remarks: It is assumed that HeapError will be called if an }
  241. { allocation fails. }
  242. {*********************************************************************}
  243. Function newpasstr(s: string): Pointer;
  244. Procedure SetupResult(Var Instr:TInstruction; operandnum: byte);
  245. {$ifdef i386}
  246. Procedure FWaitWarning;
  247. {$endif}
  248. {---------------------------------------------------------------------}
  249. { Instruction generation routines }
  250. {---------------------------------------------------------------------}
  251. { swaps in the case of a 2/3 operand opcode the destination and the }
  252. { source as to put it in AT&T style instruction format. }
  253. Procedure SwapOperands(Var instr: TInstruction);
  254. Procedure ConcatLabel(p: paasmoutput;var l : plabel);
  255. Procedure ConcatConstant(p : paasmoutput;value: longint; maxvalue: longint);
  256. Procedure ConcatRealConstant(p : paasmoutput;value: bestreal; real_typ : tfloattype);
  257. Procedure ConcatString(p : paasmoutput;s:string);
  258. Procedure ConcatPublic(p:paasmoutput;const s : string);
  259. Procedure ConcatLocal(p:paasmoutput;const s : string);
  260. Procedure ConcatGlobalBss(const s : string;size : longint);
  261. Procedure ConcatLocalBss(const s : string;size : longint);
  262. { add to list of external labels }
  263. Procedure ConcatExternal(const s : string;typ : texternal_typ);
  264. { add to internal list of labels }
  265. Procedure ConcatInternal(const s : string;typ : texternal_typ);
  266. Implementation
  267. {*************************************************************************}
  268. { Expression Parser }
  269. {*************************************************************************}
  270. Constructor TExprParse.Init;
  271. Begin
  272. end;
  273. Procedure TExprParse.Error(anerror:texpr_error);
  274. var
  275. t : tmsgconst;
  276. Begin
  277. case anerror of
  278. zero_divide: t:=assem_f_ev_zero_divide;
  279. stack_overflow: t:=assem_f_ev_stack_overflow;
  280. stack_underflow: t:=assem_f_ev_stack_underflow;
  281. invalid_number: t:=assem_f_ev_invalid_number;
  282. invalid_op: t:=assem_f_ev_invalid_op;
  283. else
  284. t:=assem_f_ev_unknown;
  285. end;
  286. Message(t);
  287. end;
  288. Procedure TExprParse.RPNPush(Num : longint); { Add an operand to the top of the RPN stack }
  289. begin
  290. if RPNTop < RPNMax then
  291. begin
  292. Inc(RPNTop);
  293. RPNStack[RPNTop] := Num;
  294. end
  295. else
  296. Error(stack_overflow); { Put some error handler here }
  297. end;
  298. Function TExprParse.RPNPop : longint; { Get the operand at the top of the RPN stack }
  299. begin
  300. if RPNTop > 0 then
  301. begin
  302. RPNPop := RPNStack[RPNTop];
  303. Dec(RPNTop);
  304. end
  305. else { Put some error handler here }
  306. Error(stack_underflow);
  307. end;
  308. Procedure TExprParse.RPNCalc(Token : String15; prefix:boolean); { RPN Calculator }
  309. Var
  310. Temp : longint;
  311. LocalError : Integer;
  312. begin
  313. { Write(Token, ' '); This just outputs the RPN expression }
  314. if (Length(Token) = 1) and (Token[1] in ['+', '-', '*', '/','&','|','%','^','~','<','>']) then
  315. Case Token[1] of { Handle operators }
  316. '+' : Begin
  317. if prefix then
  318. else
  319. RPNPush(RPNPop + RPNPop);
  320. end;
  321. '-' : Begin
  322. if prefix then
  323. RPNPush(-(RPNPop))
  324. else
  325. RPNPush(RPNPop - RPNPop);
  326. end;
  327. '*' : RPNPush(RPNPop * RPNPop);
  328. '&' : RPNPush(RPNPop AND RPNPop);
  329. '|' : RPNPush(RPNPop OR RPNPop);
  330. '~' : RPNPush(NOT RPNPop);
  331. '<' : RPNPush(RPNPop SHL RPNPop);
  332. '>' : RPNPush(RPNPop SHR RPNPop);
  333. '%' : begin
  334. Temp := RPNPop;
  335. if Temp <> 0 then
  336. RPNPush(RPNPop mod Temp)
  337. else Error(zero_divide); { Handle divide by zero error }
  338. end;
  339. '^' : RPNPush(RPNPop XOR RPNPop);
  340. '/' :
  341. begin
  342. Temp := RPNPop;
  343. if Temp <> 0 then
  344. RPNPush(RPNPop div Temp)
  345. else Error(zero_divide);{ Handle divide by 0 error }
  346. end;
  347. end
  348. else
  349. begin { Convert String to number and add to stack }
  350. if token='-2147483648' then
  351. begin
  352. temp:=$80000000;
  353. localerror:=0;
  354. end
  355. else
  356. Val(Token, Temp, LocalError);
  357. if LocalError = 0 then
  358. RPNPush(Temp)
  359. else Error(invalid_number);{ Handle error }
  360. end;
  361. end;
  362. Procedure TExprParse.OpPush(_Operator : char;prefix: boolean); { Add an operator onto top of the stack }
  363. begin
  364. if OpTop < OpMax then
  365. begin
  366. Inc(OpTop);
  367. OpStack[OpTop].ch := _Operator;
  368. OpStack[OpTop].is_prefix := prefix;
  369. end
  370. else Error(stack_overflow); { Put some error handler here }
  371. end;
  372. Procedure TExprParse.OpPop(var _Operator:TExprOperator); { Get operator at the top of the stack }
  373. begin
  374. if OpTop > 0 then
  375. begin
  376. _Operator := OpStack[OpTop];
  377. Dec(OpTop);
  378. end
  379. else Error(stack_underflow); { Put some error handler here }
  380. end;
  381. Function TExprParse.Priority(_Operator : Char) : Integer; { Return priority of operator }
  382. { The greater the priority, the higher the precedence }
  383. begin
  384. Case _Operator OF
  385. '(' : Priority := 0;
  386. '+', '-' : Priority := 1;
  387. '*', '/','%','<','>' : Priority := 2;
  388. '|','&','^','~': Priority := 0;
  389. else Error(invalid_op);{ More error handling }
  390. end;
  391. end;
  392. Function TExprParse.Evaluate(Expr : String):longint;
  393. Var
  394. I : Integer;
  395. Token : String15;
  396. opr: TExprOperator;
  397. begin
  398. OpTop := 0; { Reset stacks }
  399. RPNTop := 0;
  400. Token := '';
  401. For I := 1 to Length(Expr) DO
  402. begin
  403. if Expr[I] in ['0'..'9'] then
  404. begin { Build multi-digit numbers }
  405. Token := Token + Expr[I];
  406. if I = Length(Expr) then { Send last one to calculator }
  407. RPNCalc(Token,false);
  408. end
  409. else
  410. if Expr[I] in ['+', '-', '*', '/', '(', ')','^','&','|','%','~','<','>'] then
  411. begin
  412. if Token <> '' then
  413. begin { Send last built number to calc. }
  414. RPNCalc(Token,false);
  415. Token := '';
  416. end;
  417. Case Expr[I] OF
  418. '(' : OpPush('(',false);
  419. ')' : begin
  420. While OpStack[OpTop].ch <> '(' DO
  421. Begin
  422. OpPop(opr);
  423. RPNCalc(opr.ch,opr.is_prefix);
  424. end;
  425. OpPop(opr); { Pop off and ignore the '(' }
  426. end;
  427. '+','-','~' : Begin
  428. { workaround for -2147483648 }
  429. if (expr[I]='-') and (expr[i+1] in ['0'..'9']) then
  430. begin
  431. token:='-';
  432. expr[i]:='+';
  433. end;
  434. { if start of expression then surely a prefix }
  435. { or if previous char was also an operator }
  436. if (I = 1) or (not (Expr[I-1] in ['0'..'9','(',')'])) then
  437. OpPush(Expr[I],true)
  438. else
  439. Begin
  440. { Evaluate all higher priority operators }
  441. While (OpTop > 0) AND (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  442. Begin
  443. OpPop(opr);
  444. RPNCalc(opr.ch,opr.is_prefix);
  445. end;
  446. OpPush(Expr[I],false);
  447. End;
  448. end;
  449. '*', '/',
  450. '^','|','&',
  451. '%','<','>' : begin
  452. While (OpTop > 0) and (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  453. Begin
  454. OpPop(opr);
  455. RPNCalc(opr.ch,opr.is_prefix);
  456. end;
  457. OpPush(Expr[I],false);
  458. end;
  459. end; { Case }
  460. end
  461. else
  462. Error(invalid_op); { Handle bad input error }
  463. end;
  464. { Pop off the remaining operators }
  465. While OpTop > 0 do
  466. Begin
  467. OpPop(opr);
  468. RPNCalc(opr.ch,opr.is_prefix);
  469. end;
  470. { The result is stored on the top of the stack }
  471. Evaluate := RPNPop;
  472. end;
  473. Destructor TExprParse.Done;
  474. Begin
  475. end;
  476. Function CalculateExpression(const expression: string): longint;
  477. var
  478. expr: TExprParse;
  479. Begin
  480. expr.Init;
  481. CalculateExpression := expr.Evaluate(expression);
  482. expr.Done;
  483. end;
  484. {*************************************************************************}
  485. { String conversions/utils }
  486. {*************************************************************************}
  487. Function newpasstr(s: string): Pointer;
  488. Var
  489. StrPtr: PString;
  490. Begin
  491. GetMem(StrPtr, length(s)+1);
  492. Move(s,StrPtr^,length(s)+1);
  493. newpasstr:= Strptr;
  494. end;
  495. Function EscapeToPascal(const s:string): string;
  496. { converts a C styled string - which contains escape }
  497. { characters to a pascal style string. }
  498. var
  499. i,j: word;
  500. str: string;
  501. temp: string;
  502. value: byte;
  503. code: integer;
  504. Begin
  505. str:='';
  506. i:=1;
  507. j:=1;
  508. repeat
  509. if s[i] = '\' then
  510. Begin
  511. Inc(i);
  512. if i > 255 then
  513. Begin
  514. EscapeToPascal:=str;
  515. exit;
  516. end;
  517. case s[i] of
  518. '\': insert('\',str,j);
  519. 'b': insert(#08,str,j);
  520. 'f': insert(#12,str,j);
  521. 'n': insert(#10,str,j);
  522. 'r': insert(#13,str,j);
  523. 't': insert(#09,str,j);
  524. '"': insert('"',str,j);
  525. { octal number }
  526. '0'..'7': Begin
  527. temp:=s[i];
  528. temp:=temp+s[i+1];
  529. temp:=temp+s[i+2];
  530. inc(i,2);
  531. val(octaltodec(temp),value,code);
  532. if (code <> 0) then
  533. Message(assem_w_invalid_numeric);
  534. insert(chr(value),str,j);
  535. end;
  536. { hexadecimal number }
  537. 'x': Begin
  538. temp:=s[i+1];
  539. temp:=temp+s[i+2];
  540. inc(i,2);
  541. val(hextodec(temp),value,code);
  542. if (code <> 0) then
  543. Message(assem_w_invalid_numeric);
  544. insert(chr(value),str,j);
  545. end;
  546. else
  547. Begin
  548. Message1(assem_e_escape_seq_ignored,s[i]);
  549. insert(s[i],str,j);
  550. end;
  551. end; {end case }
  552. Inc(i);
  553. end
  554. else
  555. Begin
  556. Insert(s[i],str,j);
  557. Inc(i);
  558. if i > 255 then
  559. Begin
  560. EscapeToPascal:=str;
  561. exit;
  562. end;
  563. end;
  564. Inc(j);
  565. until (i > length(s)) or (j > 255);
  566. EscapeToPascal:=str;
  567. end;
  568. Function OctalToDec(const S:String): String;
  569. { Converts an octal string to a Decimal string }
  570. { Returns '' if there was an error. }
  571. var vs: longint;
  572. c: byte;
  573. st: string;
  574. Begin
  575. vs := 0;
  576. for c:=1 to length(s) do
  577. begin
  578. case s[c] of
  579. '0': vs:=vs shl 3;
  580. '1': vs:=vs shl 3+1;
  581. '2': vs:=vs shl 3+2;
  582. '3': vs:=vs shl 3+3;
  583. '4': vs:=vs shl 3+4;
  584. '5': vs:=vs shl 3+5;
  585. '6': vs:=vs shl 3+6;
  586. '7': vs:=vs shl 3+7;
  587. else
  588. begin
  589. Message(assem_f_error_converting_octal);
  590. OctalToDec := '';
  591. exit;
  592. end;
  593. end;
  594. end;
  595. str(vs,st);
  596. OctalToDec := st;
  597. end;
  598. Function BinaryToDec(const S:String): String;
  599. { Converts a binary string to a Decimal string }
  600. { Returns '' if there was an error. }
  601. var vs: longint;
  602. c: byte;
  603. st: string;
  604. Begin
  605. vs := 0;
  606. for c:=1 to length(s) do
  607. begin
  608. if s[c] = '0' then
  609. vs:=vs shl 1
  610. else
  611. if s[c]='1' then
  612. vs:=vs shl 1+1
  613. else
  614. begin
  615. Message(assem_f_error_converting_bin);
  616. BinaryToDec := '';
  617. exit;
  618. end;
  619. end;
  620. str(vs,st);
  621. BinaryToDec := st;
  622. end;
  623. Function HexToDec(const S:String): String;
  624. var vs: longint;
  625. c: byte;
  626. st: string;
  627. Begin
  628. vs := 0;
  629. for c:=1 to length(s) do
  630. begin
  631. case upcase(s[c]) of
  632. '0': vs:=vs shl 4;
  633. '1': vs:=vs shl 4+1;
  634. '2': vs:=vs shl 4+2;
  635. '3': vs:=vs shl 4+3;
  636. '4': vs:=vs shl 4+4;
  637. '5': vs:=vs shl 4+5;
  638. '6': vs:=vs shl 4+6;
  639. '7': vs:=vs shl 4+7;
  640. '8': vs:=vs shl 4+8;
  641. '9': vs:=vs shl 4+9;
  642. 'A': vs:=vs shl 4+10;
  643. 'B': vs:=vs shl 4+11;
  644. 'C': vs:=vs shl 4+12;
  645. 'D': vs:=vs shl 4+13;
  646. 'E': vs:=vs shl 4+14;
  647. 'F': vs:=vs shl 4+15;
  648. else
  649. begin
  650. Message(assem_f_error_converting_hex);
  651. HexToDec := '';
  652. exit;
  653. end;
  654. end;
  655. end;
  656. str(vs,st);
  657. HexToDec := st;
  658. end;
  659. Function PadZero(Var s: String; n: byte): Boolean;
  660. Begin
  661. PadZero := TRUE;
  662. { Do some error checking first }
  663. if Length(s) = n then
  664. exit
  665. else
  666. if Length(s) > n then
  667. Begin
  668. PadZero := FALSE;
  669. delete(s,n+1,length(s));
  670. exit;
  671. end
  672. else
  673. PadZero := TRUE;
  674. { Fill it up with the specified character }
  675. fillchar(s[length(s)+1],n-1,#0);
  676. {$ifndef TP}
  677. {$ifopt H+}
  678. setlength(s,n);
  679. {$else}
  680. s[0] := chr(n);
  681. {$endif}
  682. {$else}
  683. s[0] := chr(n);
  684. {$endif}
  685. end;
  686. {*************************************************************************}
  687. { Instruction utilities }
  688. {*************************************************************************}
  689. Procedure TInstruction.init;
  690. var
  691. k: integer;
  692. Begin
  693. numops := 0;
  694. labeled := FALSE;
  695. stropsize := S_NO;
  696. prefix := A_NONE;
  697. instruction := A_NONE;
  698. for k:=1 to maxoperands do
  699. begin
  700. operands[k].size := S_NO;
  701. operands[k].overriden := FALSE;
  702. operands[k].operandtype := OPR_NONE;
  703. { init to zeros }
  704. fillchar(operands[k].l, sizeof(operands[k].l),#0);
  705. end;
  706. end;
  707. Procedure TInstruction.addprefix(tok: tasmop);
  708. Begin
  709. if tok = A_NONE then
  710. Message(assem_e_syn_prefix_not_found);
  711. if Prefix = A_NONE then
  712. Prefix := tok
  713. else
  714. Message(assem_e_syn_try_add_more_prefix);
  715. end;
  716. Procedure TInstruction.addinstr(tok: tasmop);
  717. Begin
  718. if tok = A_NONE then
  719. Message(assem_e_syn_opcode_not_found);
  720. Instruction := tok;
  721. end;
  722. function TInstruction.getinstruction: tasmop;
  723. Begin
  724. getinstruction := Instruction;
  725. end;
  726. { get the current prefix of this instruction }
  727. function TInstruction.getprefix: tasmop;
  728. Begin
  729. getprefix := prefix;
  730. end;
  731. Procedure TInstruction.done;
  732. Begin
  733. end;
  734. {*************************************************************************}
  735. { Local label utilities }
  736. {*************************************************************************}
  737. Constructor TAsmLabelList.Init;
  738. Begin
  739. First := nil;
  740. Last := nil;
  741. end;
  742. Procedure TAsmLabelList.Insert(s:string; lab: PLabel; emitted: boolean);
  743. {*********************************************************************}
  744. { Description: Insert a node at the end of the list with lab and }
  745. { and the name in s. The name is allocated on the heap. }
  746. { Duplicates are not allowed. }
  747. { Indicate in emitted if this label itself has been emitted, or is it}
  748. { a simple labeled instruction? }
  749. {*********************************************************************}
  750. Begin
  751. if search(s) = nil then
  752. Begin
  753. if First = nil then
  754. Begin
  755. New(First);
  756. Last := First;
  757. end
  758. else
  759. Begin
  760. New(Last^.Next);
  761. Last := Last^.Next;
  762. end;
  763. Last^.name := NewPasStr(s);
  764. Last^.Lab := lab;
  765. Last^.Next := nil;
  766. Last^.emitted := emitted;
  767. end;
  768. end;
  769. Function TAsmLabelList.Search(const s: string): PAsmLabel;
  770. {*********************************************************************}
  771. { Description: This routine searches for a label named s in the }
  772. { linked list, returns a pointer to the label if found, otherwise }
  773. { returns nil. }
  774. {*********************************************************************}
  775. Var
  776. asmlab: PAsmLabel;
  777. Begin
  778. asmlab := First;
  779. if First = nil then
  780. Begin
  781. Search := nil;
  782. exit;
  783. end;
  784. While (asmlab^.name^ <> s) and (asmlab^.Next <> nil) do
  785. asmlab := asmlab^.Next;
  786. if asmlab^.name^ = s then
  787. search := asmlab
  788. else
  789. search := nil;
  790. end;
  791. Destructor TAsmLabelList.Done;
  792. {*********************************************************************}
  793. { Description: This routine takes care of deallocating all nodes }
  794. { in the linked list, as well as deallocating the string pointers }
  795. { of these nodes. }
  796. { }
  797. { Remark: The PLabel field is NOT freed, the compiler takes care of }
  798. { this. }
  799. {*********************************************************************}
  800. Var
  801. temp: PAsmLabel;
  802. temp1: PAsmLabel;
  803. Begin
  804. temp := First;
  805. while temp <> nil do
  806. Begin
  807. Freemem(Temp^.name, length(Temp^.name^)+1);
  808. Temp1 := Temp^.Next;
  809. Dispose(Temp);
  810. Temp := Temp1;
  811. { The plabel could be deleted here, but let us not do }
  812. { it, FPC will do it instead. }
  813. end;
  814. end;
  815. Function TAsmLabelList.newpasstr(s: string): PString;
  816. {*********************************************************************}
  817. { FUNCTION NewPasStr(s:string): PString }
  818. { Description: This routine allocates a string on the heap and }
  819. { returns a pointer to the allocated string. }
  820. { }
  821. { Remarks: The string allocated should not be modified, since it's }
  822. { length will be less then 255. }
  823. { Remarks: It is assumed that HeapError will be called if an }
  824. { allocation fails. }
  825. {*********************************************************************}
  826. Var
  827. StrPtr: PString;
  828. Begin
  829. GetMem(StrPtr, length(s)+1);
  830. Move(s,StrPtr^,length(s)+1);
  831. newpasstr:= Strptr;
  832. end;
  833. {*************************************************************************}
  834. { Symbol table helper routines }
  835. {*************************************************************************}
  836. Procedure SwapOperands(Var instr: TInstruction);
  837. Var
  838. tempopr: TOperand;
  839. Begin
  840. if instr.numops = 2 then
  841. Begin
  842. tempopr := instr.operands[1];
  843. instr.operands[1] := instr.operands[2];
  844. instr.operands[2] := tempopr;
  845. end
  846. else
  847. if instr.numops = 3 then
  848. Begin
  849. tempopr := instr.operands[1];
  850. instr.operands[1] := instr.operands[3];
  851. instr.operands[3] := tempopr;
  852. end;
  853. end;
  854. Function SearchIConstant(const s:string; var l:longint): boolean;
  855. {**********************************************************************}
  856. { Description: Searches for a CONSTANT of name s in either the local }
  857. { symbol list, then in the global symbol list, and returns the value }
  858. { of that constant in l. Returns TRUE if successfull, if not found, }
  859. { or if the constant is not of correct type, then returns FALSE }
  860. { Remarks: Also handle TRUE and FALSE returning in those cases 1 and 0 }
  861. { respectively. }
  862. {**********************************************************************}
  863. var
  864. sym: psym;
  865. Begin
  866. SearchIConstant := FALSE;
  867. { check for TRUE or FALSE reserved words first }
  868. if s = 'TRUE' then
  869. Begin
  870. SearchIConstant := TRUE;
  871. l := 1;
  872. end
  873. else
  874. if s = 'FALSE' then
  875. Begin
  876. SearchIConstant := TRUE;
  877. l := 0;
  878. end
  879. else
  880. if assigned(aktprocsym) then
  881. Begin
  882. if assigned(aktprocsym^.definition) then
  883. Begin
  884. { Check the local constants }
  885. if assigned(aktprocsym^.definition^.localst) and
  886. (lexlevel >= normal_function_level) then
  887. sym := aktprocsym^.definition^.localst^.search(s)
  888. else
  889. sym := nil;
  890. if assigned(sym) then
  891. Begin
  892. if (sym^.typ = constsym) and
  893. (pconstsym(sym)^.consttype in [constord,constint,constchar,constbool]) then
  894. Begin
  895. l:=pconstsym(sym)^.value;
  896. SearchIConstant := TRUE;
  897. exit;
  898. end;
  899. end;
  900. end;
  901. end;
  902. { Check the global constants }
  903. getsym(s,false);
  904. if srsym <> nil then
  905. Begin
  906. case srsym^.typ of
  907. constsym :
  908. begin
  909. if (pconstsym(srsym)^.consttype in [constord,constint,constchar,constbool]) then
  910. Begin
  911. l:=pconstsym(srsym)^.value;
  912. SearchIConstant := TRUE;
  913. exit;
  914. end;
  915. end;
  916. end;
  917. end;
  918. end;
  919. Procedure SetupResult(Var Instr:TInstruction; operandnum: byte);
  920. {**********************************************************************}
  921. { Description: This routine changes the correct fields and correct }
  922. { offset in the reference, so that it points to the __RESULT or }
  923. { @Result variable (depending on the inline asm). }
  924. { Resturns a reference with all correct offset correctly set up. }
  925. { The Operand should already point to a treference on entry. }
  926. {**********************************************************************}
  927. Begin
  928. { replace by correct offset. }
  929. if assigned(procinfo.retdef) and
  930. (procinfo.retdef<>pdef(voiddef)) then
  931. begin
  932. instr.operands[operandnum].ref.offset := procinfo.retoffset;
  933. instr.operands[operandnum].ref.base := procinfo.framepointer;
  934. { always assume that the result is valid. }
  935. procinfo.funcret_is_valid:=true;
  936. end
  937. else
  938. Message(assem_e_invalid_symbol_ref);
  939. end;
  940. {$ifdef i386}
  941. Procedure FWaitWarning;
  942. begin
  943. if (target_info.target=target_i386_GO32V2) and (cs_fp_emulation in aktmoduleswitches) then
  944. Message(assem_w_fwait_emu_prob);
  945. end;
  946. {$endif i386}
  947. Procedure SetOperandSize(var instr:TInstruction;operandnum,size:longint);
  948. begin
  949. { the current size is NOT overriden if it already }
  950. { exists, such as in the case of a byte ptr, in }
  951. { front of the identifier. }
  952. if (instr.operands[operandnum].size = S_NO) or (instr.operands[operandnum].overriden = FALSE) then
  953. Begin
  954. case size of
  955. 1: instr.operands[operandnum].size := S_B;
  956. 2: instr.operands[operandnum].size := S_W{ could be S_IS};
  957. 4: instr.operands[operandnum].size := S_L{ could be S_IL or S_FS};
  958. 8: instr.operands[operandnum].size := S_IQ{ could be S_D or S_FL};
  959. extended_size: instr.operands[operandnum].size := S_FX;
  960. else
  961. { this is in the case where the instruction is LEA }
  962. { or something like that, in that case size is not }
  963. { important. }
  964. instr.operands[operandnum].size := S_NO;
  965. end; { end case }
  966. end;
  967. end;
  968. Function GetVarOffsetSize(const base,field:string;Var Offset: longint;var Size:longint):boolean;
  969. { search and returns the offset and size of records/objects of the base }
  970. { with field name setup in field. }
  971. { returns FALSE if not found. }
  972. { used when base is a variable or a typed constant name. }
  973. var
  974. sym:psym;
  975. p: psym;
  976. Begin
  977. GetVarOffsetSize := FALSE;
  978. Offset := 0;
  979. { local list }
  980. if assigned(aktprocsym) then
  981. begin
  982. if assigned(aktprocsym^.definition^.localst) then
  983. sym:=aktprocsym^.definition^.localst^.search(base)
  984. else
  985. sym:=nil;
  986. if assigned(sym) then
  987. begin
  988. { field of local record variable. }
  989. if (sym^.typ=varsym) and (pvarsym(sym)^.definition^.deftype=recorddef) then
  990. begin
  991. p:=pvarsym(precdef(pvarsym(sym)^.definition)^.symtable^.search(field));
  992. if assigned(pvarsym(p)) then
  993. Begin
  994. Offset := pvarsym(p)^.address;
  995. Size:=PVarsym(p)^.getsize;
  996. GetVarOffsetSize := TRUE;
  997. Exit;
  998. end;
  999. end
  1000. else
  1001. if (sym^.typ=varsym) and (pvarsym(sym)^.definition^.deftype=objectdef) then
  1002. begin
  1003. if assigned(pobjectdef(pvarsym(sym)^.definition)^.publicsyms) then
  1004. begin
  1005. p:=pvarsym(pobjectdef(pvarsym(sym)^.definition)^.publicsyms^.search(field));
  1006. if assigned(pvarsym(p)) then
  1007. Begin
  1008. Offset := pvarsym(p)^.address;
  1009. Size:=PVarsym(p)^.getsize;
  1010. GetVarOffsetSize := TRUE;
  1011. Exit;
  1012. end;
  1013. end;
  1014. end;
  1015. end
  1016. else
  1017. begin
  1018. { field of local record parameter to routine. }
  1019. if assigned(aktprocsym^.definition^.parast) then
  1020. sym:=aktprocsym^.definition^.parast^.search(base)
  1021. else
  1022. sym:=nil;
  1023. if assigned(sym) then
  1024. begin
  1025. if (sym^.typ=varsym) and (pvarsym(sym)^.definition^.deftype=recorddef)
  1026. then
  1027. begin
  1028. p:=pvarsym(precdef(pvarsym(sym)^.definition)^.symtable^.search(field));
  1029. if assigned(p) then
  1030. Begin
  1031. Offset := pvarsym(p)^.address;
  1032. Size:=PVarsym(p)^.getsize;
  1033. GetVarOffsetSize := TRUE;
  1034. Exit;
  1035. end;
  1036. end { endif }
  1037. else
  1038. if (sym^.typ=varsym) and (pvarsym(sym)^.definition^.deftype=objectdef) then
  1039. begin
  1040. if assigned(pobjectdef(pvarsym(sym)^.definition)^.publicsyms) then
  1041. begin
  1042. p:=pvarsym(pobjectdef(pvarsym(sym)^.definition)^.publicsyms^.search(field));
  1043. if assigned(pvarsym(p)) then
  1044. Begin
  1045. Offset := pvarsym(p)^.address;
  1046. Size:=PVarsym(p)^.getsize;
  1047. GetVarOffsetSize := TRUE;
  1048. Exit;
  1049. end;
  1050. end;
  1051. end;
  1052. end;
  1053. end;
  1054. end; { endif assigned(aktprocsym) }
  1055. { not found.. .now look for global variables. }
  1056. getsym(base,false);
  1057. sym:=srsym;
  1058. if assigned(sym) then
  1059. Begin
  1060. { field of global record variable. }
  1061. if (sym^.typ=varsym) and (pvarsym(sym)^.definition^.deftype=recorddef) then
  1062. begin
  1063. p:=pvarsym(precdef(pvarsym(sym)^.definition)^.symtable^.search(field));
  1064. if assigned(p) then
  1065. Begin
  1066. Offset := pvarsym(p)^.address;
  1067. Size:=PVarsym(p)^.getsize;
  1068. GetVarOffsetSize := TRUE;
  1069. Exit;
  1070. end;
  1071. end
  1072. else
  1073. { field of global record type constant. }
  1074. if (sym^.typ=typedconstsym) and (ptypedconstsym(sym)^.definition^.deftype=recorddef)
  1075. then
  1076. begin
  1077. p:=pvarsym(precdef(pvarsym(sym)^.definition)^.symtable^.search(field));
  1078. if assigned(p) then
  1079. Begin
  1080. Offset := pvarsym(p)^.address;
  1081. Size:=PVarsym(p)^.getsize;
  1082. GetVarOffsetSize := TRUE;
  1083. Exit;
  1084. end;
  1085. end
  1086. else
  1087. if (sym^.typ=varsym) and (pvarsym(sym)^.definition^.deftype=objectdef) then
  1088. begin
  1089. if assigned(pobjectdef(pvarsym(sym)^.definition)^.publicsyms) then
  1090. begin
  1091. p:=pvarsym(pobjectdef(pvarsym(sym)^.definition)^.publicsyms^.search(field));
  1092. if assigned(pvarsym(p)) then
  1093. Begin
  1094. Offset := pvarsym(p)^.address;
  1095. Size:=PVarsym(p)^.getsize;
  1096. GetVarOffsetSize := TRUE;
  1097. Exit;
  1098. end;
  1099. end;
  1100. end;
  1101. end; { end looking for global variables .. }
  1102. end;
  1103. Function GetTypeOffsetSize(const base,field: string;Var Offset: longint;var Size:longint):boolean;
  1104. { search and returns the offset of records/objects of the base }
  1105. { with field name setup in field. }
  1106. { returns 0 if not found. }
  1107. { used when base is a variable or a typed constant name. }
  1108. var
  1109. sym:psym;
  1110. p: psym;
  1111. Begin
  1112. Offset := 0;
  1113. GetTypeOffsetSize := FALSE;
  1114. { local list }
  1115. if assigned(aktprocsym) then
  1116. begin
  1117. if assigned(aktprocsym^.definition^.localst) then
  1118. sym:=aktprocsym^.definition^.localst^.search(base)
  1119. else
  1120. sym:=nil;
  1121. if assigned(sym) then
  1122. begin
  1123. { field of local record type. }
  1124. if (sym^.typ=typesym) and (ptypesym(sym)^.definition^.deftype=recorddef) then
  1125. begin
  1126. p:=precdef(ptypesym(sym)^.definition)^.symtable^.search(field);
  1127. if assigned(p) then
  1128. Begin
  1129. Offset := pvarsym(p)^.address;
  1130. Size:=PVarsym(p)^.getsize;
  1131. GetTypeOffsetSize := TRUE;
  1132. Exit;
  1133. end;
  1134. end;
  1135. end
  1136. else
  1137. begin
  1138. { field of local record type to routine. }
  1139. if assigned(aktprocsym^.definition^.parast) then
  1140. sym:=aktprocsym^.definition^.parast^.search(base)
  1141. else
  1142. sym:=nil;
  1143. if assigned(sym) then
  1144. begin
  1145. if (sym^.typ=typesym) and (ptypesym(sym)^.definition^.deftype=recorddef)
  1146. then
  1147. begin
  1148. p:=precdef(ptypesym(sym)^.definition)^.symtable^.search(field);
  1149. if assigned(p) then
  1150. Begin
  1151. Offset := pvarsym(p)^.address;
  1152. GetTypeOffsetSize := TRUE;
  1153. Exit;
  1154. end;
  1155. end; { endif }
  1156. end; {endif }
  1157. end; { endif }
  1158. end;
  1159. { not found.. .now look for global types. }
  1160. getsym(base,false);
  1161. sym:=srsym;
  1162. if assigned(sym) then
  1163. Begin
  1164. { field of global record types. }
  1165. if (sym^.typ=typesym) and (ptypesym(sym)^.definition^.deftype=recorddef) then
  1166. begin
  1167. p:=precdef(ptypesym(sym)^.definition)^.symtable^.search(field);
  1168. if assigned(p) then
  1169. Begin
  1170. Offset := pvarsym(p)^.address;
  1171. Size:=PVarsym(p)^.getsize;
  1172. GetTypeOffsetSize := TRUE;
  1173. Exit;
  1174. end
  1175. end
  1176. else
  1177. { public field names of objects }
  1178. if (sym^.typ=typesym) and (ptypesym(sym)^.definition^.deftype=objectdef)then
  1179. begin
  1180. if assigned(pobjectdef(ptypesym(sym)^.definition)^.publicsyms) then
  1181. Begin
  1182. p:=pobjectdef(ptypesym(sym)^.definition)^.publicsyms^.search(field);
  1183. if assigned(p) then
  1184. Begin
  1185. Offset := pvarsym(p)^.address;
  1186. Size:=PVarsym(p)^.getsize;
  1187. GetTypeOffsetSize := TRUE;
  1188. Exit;
  1189. end
  1190. end;
  1191. end;
  1192. end; { end looking for global variables .. }
  1193. end;
  1194. Function CreateVarInstr(var Instr: TInstruction; const hs:string;operandnum:byte): Boolean;
  1195. { search and sets up the correct fields in the Instr record }
  1196. { for the NON-constant identifier passed to the routine. }
  1197. { if not found returns FALSE. }
  1198. var
  1199. sym : psym;
  1200. l : longint;
  1201. Begin
  1202. CreateVarInstr := FALSE;
  1203. { are we in a routine ? }
  1204. if assigned(aktprocsym) then
  1205. begin
  1206. { search the local list for the name of this variable. }
  1207. if assigned(aktprocsym^.definition^.localst) then
  1208. sym:=aktprocsym^.definition^.localst^.search(hs)
  1209. else
  1210. sym:=nil;
  1211. if assigned(sym) then
  1212. begin
  1213. case sym^.typ of
  1214. varsym : begin
  1215. { we always assume in asm statements that }
  1216. { that the variable is valid. }
  1217. pvarsym(sym)^.is_valid:=1;
  1218. if pvarsym(sym)^.owner^.symtabletype=staticsymtable then
  1219. begin
  1220. instr.operands[operandnum].ref.symbol:=newasmsymbol(pvarsym(sym)^.mangledname);
  1221. end
  1222. else
  1223. begin
  1224. instr.operands[operandnum].ref.base := procinfo.framepointer;
  1225. instr.operands[operandnum].ref.offset := -(pvarsym(sym)^.address);
  1226. end;
  1227. { the current size is NOT overriden if it already }
  1228. { exists, such as in the case of a byte ptr, in }
  1229. { front of the identifier. }
  1230. if (instr.operands[operandnum].size = S_NO) or (instr.operands[operandnum].overriden = FALSE) then
  1231. Begin
  1232. case pvarsym(sym)^.getsize of
  1233. 1: instr.operands[operandnum].size := S_B;
  1234. 2: instr.operands[operandnum].size := S_W{ could be S_IS};
  1235. 4: instr.operands[operandnum].size := S_L{ could be S_IL or S_FS};
  1236. 8: instr.operands[operandnum].size := S_IQ{ could be S_D or S_FL};
  1237. extended_size: instr.operands[operandnum].size := S_FX;
  1238. else
  1239. { this is in the case where the instruction is LEA }
  1240. { or something like that, in that case size is not }
  1241. { important. }
  1242. instr.operands[operandnum].size := S_NO;
  1243. end; { end case }
  1244. end;
  1245. { ok, finished for this var }
  1246. CreateVarInstr := TRUE;
  1247. Exit;
  1248. end;
  1249. typedconstsym : begin
  1250. { we always assume in asm statements that }
  1251. { that the variable is valid. }
  1252. instr.operands[operandnum].ref.symbol:=newasmsymbol(pvarsym(sym)^.mangledname);
  1253. { the current size is NOT overriden if it already }
  1254. { exists, such as in the case of a byte ptr, in }
  1255. { front of the identifier. }
  1256. if (instr.operands[operandnum].size = S_NO) or (instr.operands[operandnum].overriden = FALSE) then
  1257. Begin
  1258. case ptypedconstsym(sym)^.getsize of
  1259. 1: instr.operands[operandnum].size := S_B;
  1260. 2: instr.operands[operandnum].size := S_W{ could be S_IS};
  1261. 4: instr.operands[operandnum].size := S_L{ could be S_IL or S_FS};
  1262. 8: instr.operands[operandnum].size := S_IQ{ could be S_D or S_FL};
  1263. extended_size: instr.operands[operandnum].size := S_FX;
  1264. else
  1265. instr.operands[operandnum].size := S_NO;
  1266. end; { end case }
  1267. end;
  1268. { ok, finished for this var }
  1269. CreateVarInstr := TRUE;
  1270. Exit;
  1271. end;
  1272. constsym : begin
  1273. if pconstsym(sym)^.consttype in [constint,constchar,constbool] then
  1274. begin
  1275. instr.operands[operandnum].operandtype:=OPR_CONSTANT;
  1276. instr.operands[operandnum].val:=pconstsym(sym)^.value;
  1277. CreateVarInstr := TRUE;
  1278. Exit;
  1279. end;
  1280. end;
  1281. typesym : begin
  1282. if ptypesym(sym)^.definition^.deftype in [recorddef,objectdef] then
  1283. begin
  1284. instr.operands[operandnum].operandtype:=OPR_CONSTANT;
  1285. instr.operands[operandnum].val:=0;
  1286. CreateVarInstr := TRUE;
  1287. Exit;
  1288. end;
  1289. end;
  1290. procsym : begin
  1291. instr.operands[operandnum].operandtype:=OPR_SYMBOL;
  1292. instr.operands[operandnum].symbol:=newasmsymbol(pprocsym(sym)^.definition^.mangledname);
  1293. CreateVarInstr := TRUE;
  1294. Exit;
  1295. end
  1296. else
  1297. begin
  1298. Message(assem_e_unsupported_symbol_type);
  1299. exit;
  1300. end;
  1301. end;
  1302. end;
  1303. { now check for parameters passed to routine }
  1304. if assigned(aktprocsym^.definition^.parast) then
  1305. sym:=aktprocsym^.definition^.parast^.search(hs)
  1306. else
  1307. sym:=nil;
  1308. if assigned(sym) then
  1309. begin
  1310. case sym^.typ of
  1311. varsym : begin
  1312. if pvarsym(sym)^.islocalcopy then
  1313. l:=-pvarsym(sym)^.address
  1314. else
  1315. begin
  1316. l:=pvarsym(sym)^.address;
  1317. { set offset }
  1318. inc(l,aktprocsym^.definition^.parast^.call_offset);
  1319. end;
  1320. pvarsym(sym)^.is_valid:=1;
  1321. instr.operands[operandnum].ref.base := procinfo.framepointer;
  1322. instr.operands[operandnum].ref.offset := l;
  1323. { the current size is NOT overriden if it already }
  1324. { exists, such as in the case of a byte ptr, in }
  1325. { front of the identifier. }
  1326. if (instr.operands[operandnum].size = S_NO) or (instr.operands[operandnum].overriden = FALSE) then
  1327. Begin
  1328. case pvarsym(sym)^.getsize of
  1329. 1: instr.operands[operandnum].size := S_B;
  1330. 2: instr.operands[operandnum].size := S_W;
  1331. 4: instr.operands[operandnum].size := S_L;
  1332. 8: instr.operands[operandnum].size := S_IQ;
  1333. extended_size: instr.operands[operandnum].size := S_FX;
  1334. else
  1335. { this is in the case where the instruction is LEA }
  1336. { or something like that, in that case size is not }
  1337. { important. }
  1338. instr.operands[operandnum].size := S_NO;
  1339. end; { end case }
  1340. end; { endif }
  1341. CreateVarInstr := TRUE;
  1342. Exit;
  1343. end;
  1344. else
  1345. begin
  1346. Message(assem_e_unsupported_symbol_type);
  1347. exit;
  1348. end;
  1349. end; { case }
  1350. end; { endif }
  1351. end;
  1352. { not found.. .now look for global variables. }
  1353. getsym(hs,false);
  1354. sym:=srsym;
  1355. if assigned(sym) then
  1356. Begin
  1357. case sym^.typ of
  1358. varsym,
  1359. typedconstsym : Begin
  1360. instr.operands[operandnum].ref.symbol:=newasmsymbol(sym^.mangledname);
  1361. { the current size is NOT overriden if it already }
  1362. { exists, such as in the case of a byte ptr, in }
  1363. { front of the identifier. }
  1364. if (instr.operands[operandnum].size = S_NO) or (instr.operands[operandnum].overriden = FALSE) then
  1365. Begin
  1366. case pvarsym(sym)^.getsize of
  1367. 1: instr.operands[operandnum].size := S_B;
  1368. 2: instr.operands[operandnum].size := S_W;
  1369. 4: instr.operands[operandnum].size := S_L;
  1370. 8: instr.operands[operandnum].size := S_IQ;
  1371. else
  1372. { this is in the case where the instruction is LEA }
  1373. { or something like that, in that case size is not }
  1374. { important. }
  1375. instr.operands[operandnum].size := S_NO;
  1376. end;
  1377. end
  1378. else
  1379. if (instr.operands[operandnum].size = S_NO) and (sym^.typ = typedconstsym) then
  1380. Begin
  1381. { only these are valid sizes, otherwise prefixes are }
  1382. { required. }
  1383. case ptypedconstsym(sym)^.definition^.size of
  1384. 1: instr.operands[operandnum].size := S_B;
  1385. 2: instr.operands[operandnum].size := S_W;
  1386. 4: instr.operands[operandnum].size := S_L;
  1387. 8: instr.operands[operandnum].size := S_IQ;
  1388. else
  1389. { this is in the case where the instruction is LEA }
  1390. { or something like that, in that case size is not }
  1391. { important. }
  1392. instr.operands[operandnum].size := S_NO;
  1393. end;
  1394. end; { endif }
  1395. CreateVarInstr := TRUE;
  1396. Exit;
  1397. end;
  1398. constsym : begin
  1399. if pconstsym(sym)^.consttype in [constint,constchar,constbool] then
  1400. begin
  1401. instr.operands[operandnum].operandtype:=OPR_CONSTANT;
  1402. instr.operands[operandnum].val:=pconstsym(sym)^.value;
  1403. CreateVarInstr := TRUE;
  1404. Exit;
  1405. end;
  1406. end;
  1407. typesym : begin
  1408. if ptypesym(sym)^.definition^.deftype in [recorddef,objectdef] then
  1409. begin
  1410. instr.operands[operandnum].operandtype:=OPR_CONSTANT;
  1411. instr.operands[operandnum].val:=0;
  1412. CreateVarInstr := TRUE;
  1413. Exit;
  1414. end;
  1415. end;
  1416. procsym : begin
  1417. if assigned(pprocsym(sym)^.definition^.nextoverloaded) then
  1418. Message(assem_w_calling_overload_func);
  1419. instr.operands[operandnum].operandtype:=OPR_SYMBOL;
  1420. instr.operands[operandnum].size:=S_L;
  1421. instr.operands[operandnum].symbol:=newasmsymbol(pprocsym(sym)^.definition^.mangledname);
  1422. CreateVarInstr := TRUE;
  1423. Exit;
  1424. end;
  1425. else
  1426. begin
  1427. Message(assem_e_unsupported_symbol_type);
  1428. exit;
  1429. end;
  1430. end; {case}
  1431. end; { end looking for global variables .. }
  1432. end;
  1433. Function SearchLabel(const s: string; var hl: plabel): boolean;
  1434. {**********************************************************************}
  1435. { Description: Searches for a pascal label definition, first in the }
  1436. { local symbol list and then in the global symbol list. If found then }
  1437. { return pointer to label and return true, otherwise returns false. }
  1438. {**********************************************************************}
  1439. var
  1440. sym: psym;
  1441. Begin
  1442. SearchLabel := FALSE;
  1443. if assigned(aktprocsym) then
  1444. Begin
  1445. { Check the local constants }
  1446. if assigned(aktprocsym^.definition) then
  1447. Begin
  1448. if assigned(aktprocsym^.definition^.localst) then
  1449. sym := aktprocsym^.definition^.localst^.search(s)
  1450. else
  1451. sym := nil;
  1452. if assigned(sym) then
  1453. Begin
  1454. if (sym^.typ = labelsym) then
  1455. Begin
  1456. hl:=plabelsym(sym)^.number;
  1457. SearchLabel := TRUE;
  1458. exit;
  1459. end;
  1460. end;
  1461. end;
  1462. end;
  1463. { Check the global label symbols... }
  1464. getsym(s,false);
  1465. if srsym <> nil then
  1466. Begin
  1467. if (srsym^.typ=labelsym) then
  1468. Begin
  1469. hl:=plabelsym(srsym)^.number;
  1470. SearchLabel:= TRUE;
  1471. exit;
  1472. end;
  1473. end;
  1474. end;
  1475. {*************************************************************************}
  1476. { Instruction Generation Utilities }
  1477. {*************************************************************************}
  1478. Procedure ConcatString(p : paasmoutput;s:string);
  1479. {*********************************************************************}
  1480. { PROCEDURE ConcatString(s:string); }
  1481. { Description: This routine adds the character chain pointed to in }
  1482. { s to the instruction linked list. }
  1483. {*********************************************************************}
  1484. Var
  1485. pc: PChar;
  1486. Begin
  1487. getmem(pc,length(s)+1);
  1488. p^.concat(new(pai_string,init_length_pchar(strpcopy(pc,s),length(s))));
  1489. end;
  1490. Procedure ConcatPasString(p : paasmoutput;s:string);
  1491. {*********************************************************************}
  1492. { PROCEDURE ConcatPasString(s:string); }
  1493. { Description: This routine adds the character chain pointed to in }
  1494. { s to the instruction linked list, contrary to ConcatString it }
  1495. { uses a pascal style string, so it conserves null characters. }
  1496. {*********************************************************************}
  1497. Begin
  1498. p^.concat(new(pai_string,init(s)));
  1499. end;
  1500. Procedure ConcatDirect(p : paasmoutput;s:string);
  1501. {*********************************************************************}
  1502. { PROCEDURE ConcatDirect(s:string) }
  1503. { Description: This routine output the string directly to the asm }
  1504. { output, it is only sed when writing special labels in AT&T mode, }
  1505. { and should not be used without due consideration, since it may }
  1506. { cause problems. }
  1507. {*********************************************************************}
  1508. Var
  1509. pc: PChar;
  1510. Begin
  1511. getmem(pc,length(s)+1);
  1512. p^.concat(new(pai_direct,init(strpcopy(pc,s))));
  1513. end;
  1514. Procedure ConcatConstant(p: paasmoutput; value: longint; maxvalue: longint);
  1515. {*********************************************************************}
  1516. { PROCEDURE ConcatConstant(value: longint; maxvalue: longint); }
  1517. { Description: This routine adds the value constant to the current }
  1518. { instruction linked list. }
  1519. { maxvalue -> indicates the size of the data to initialize: }
  1520. { $ff -> create a byte node. }
  1521. { $ffff -> create a word node. }
  1522. { $ffffffff -> create a dword node. }
  1523. {*********************************************************************}
  1524. Begin
  1525. if value > maxvalue then
  1526. Begin
  1527. Message(assem_e_constant_out_of_bounds);
  1528. { assuming a value of maxvalue }
  1529. value := maxvalue;
  1530. end;
  1531. if maxvalue = $ff then
  1532. p^.concat(new(pai_const,init_8bit(byte(value))))
  1533. else
  1534. if maxvalue = $ffff then
  1535. p^.concat(new(pai_const,init_16bit(word(value))))
  1536. else
  1537. if maxvalue = $ffffffff then
  1538. p^.concat(new(pai_const,init_32bit(longint(value))));
  1539. end;
  1540. Procedure ConcatRealConstant(p : paasmoutput;value: bestreal; real_typ : tfloattype);
  1541. {***********************************************************************}
  1542. { PROCEDURE ConcatRealConstant(value: bestreal; real_typ : tfloattype); }
  1543. { Description: This routine adds the value constant to the current }
  1544. { instruction linked list. }
  1545. { real_typ -> indicates the type of the real data to initialize: }
  1546. { s32real -> create a single node. }
  1547. { s64real -> create a double node. }
  1548. { s80real -> create an extended node. }
  1549. { s64bit -> create a comp node. }
  1550. { f32bit -> create a fixed node. (not used normally) }
  1551. {***********************************************************************}
  1552. Begin
  1553. case real_typ of
  1554. s32real : p^.concat(new(pai_single,init(value)));
  1555. s64real : p^.concat(new(pai_double,init(value)));
  1556. s80real : p^.concat(new(pai_extended,init(value)));
  1557. s64bit : p^.concat(new(pai_comp,init(value)));
  1558. f32bit : p^.concat(new(pai_const,init_32bit(trunc(value*$10000))));
  1559. end;
  1560. end;
  1561. Procedure ConcatLabel(p: paasmoutput;var l : plabel);
  1562. {*********************************************************************}
  1563. { PROCEDURE ConcatLabel }
  1564. { Description: This routine either emits a label or a labeled }
  1565. { instruction to the linked list of instructions. }
  1566. {*********************************************************************}
  1567. begin
  1568. p^.concat(new(pai_label,init(l)))
  1569. end;
  1570. procedure ConcatPublic(p:paasmoutput;const s : string);
  1571. {*********************************************************************}
  1572. { PROCEDURE ConcatPublic }
  1573. { Description: This routine emits an global definition to the }
  1574. { linked list of instructions.(used by AT&T styled asm) }
  1575. {*********************************************************************}
  1576. begin
  1577. p^.concat(new(pai_symbol,init_global(s)));
  1578. { concat_internal(s,EXT_NEAR); done in aasm }
  1579. end;
  1580. procedure ConcatLocal(p:paasmoutput;const s : string);
  1581. {*********************************************************************}
  1582. { PROCEDURE ConcatLocal }
  1583. { Description: This routine emits an local definition to the }
  1584. { linked list of instructions. }
  1585. {*********************************************************************}
  1586. begin
  1587. p^.concat(new(pai_symbol,init(s)));
  1588. { concat_internal(s,EXT_NEAR); done in aasm }
  1589. end;
  1590. Procedure ConcatGlobalBss(const s : string;size : longint);
  1591. {*********************************************************************}
  1592. { PROCEDURE ConcatGlobalBss }
  1593. { Description: This routine emits an global datablock to the }
  1594. { linked list of instructions. }
  1595. {*********************************************************************}
  1596. begin
  1597. bsssegment^.concat(new(pai_datablock,init_global(s,size)));
  1598. { concat_internal(s,EXT_NEAR); done in aasm }
  1599. end;
  1600. Procedure ConcatLocalBss(const s : string;size : longint);
  1601. {*********************************************************************}
  1602. { PROCEDURE ConcatLocalBss }
  1603. { Description: This routine emits a local datablcok to the }
  1604. { linked list of instructions. }
  1605. {*********************************************************************}
  1606. begin
  1607. bsssegment^.concat(new(pai_datablock,init(s,size)));
  1608. { concat_internal(s,EXT_NEAR); done in aasm }
  1609. end;
  1610. { add to list of external labels }
  1611. Procedure ConcatExternal(const s : string;typ : texternal_typ);
  1612. {*********************************************************************}
  1613. { PROCEDURE ConcatExternal }
  1614. { Description: This routine emits an external definition to the }
  1615. { linked list of instructions.(used by AT&T styled asm) }
  1616. {*********************************************************************}
  1617. { check if in internal list and remove it there }
  1618. var p : pai_external;
  1619. begin
  1620. p:=search_assembler_symbol(internals,s,typ);
  1621. if p<>nil then internals^.remove(p);
  1622. concat_external(s,typ);
  1623. end;
  1624. { add to internal list of labels }
  1625. Procedure ConcatInternal(const s : string;typ : texternal_typ);
  1626. {*********************************************************************}
  1627. { PROCEDURE ConcatInternal }
  1628. { Description: This routine emits an internal definition of a symbol }
  1629. { (used by AT&T styled asm for undefined labels) }
  1630. {*********************************************************************}
  1631. begin
  1632. concat_internal(s,typ);
  1633. end;
  1634. end.
  1635. {
  1636. $Log$
  1637. Revision 1.5 1999-02-25 21:02:51 peter
  1638. * ag386bin updates
  1639. + coff writer
  1640. Revision 1.4 1999/02/22 02:15:39 peter
  1641. * updates for ag386bin
  1642. Revision 1.3 1999/02/16 00:47:28 peter
  1643. * fixed local copies of value para's
  1644. Revision 1.2 1999/01/27 13:04:11 pierre
  1645. * bug with static vars in assembler readers
  1646. Revision 1.1 1999/01/10 15:38:00 peter
  1647. * moved some tables from ra386*.pas -> i386.pas
  1648. + start of coff writer
  1649. * renamed asmutils unit to rautils
  1650. }