rautils.pas 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814
  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. inc(pvarsym(sym)^.refs);
  1219. if pvarsym(sym)^.owner^.symtabletype=staticsymtable then
  1220. begin
  1221. instr.operands[operandnum].ref.symbol:=newasmsymbol(pvarsym(sym)^.mangledname);
  1222. end
  1223. else
  1224. begin
  1225. instr.operands[operandnum].ref.base := procinfo.framepointer;
  1226. instr.operands[operandnum].ref.offset := -(pvarsym(sym)^.address);
  1227. end;
  1228. { the current size is NOT overriden if it already }
  1229. { exists, such as in the case of a byte ptr, in }
  1230. { front of the identifier. }
  1231. if (instr.operands[operandnum].size = S_NO) or (instr.operands[operandnum].overriden = FALSE) then
  1232. Begin
  1233. case pvarsym(sym)^.getsize of
  1234. 1: instr.operands[operandnum].size := S_B;
  1235. 2: instr.operands[operandnum].size := S_W{ could be S_IS};
  1236. 4: instr.operands[operandnum].size := S_L{ could be S_IL or S_FS};
  1237. 8: instr.operands[operandnum].size := S_IQ{ could be S_D or S_FL};
  1238. extended_size: instr.operands[operandnum].size := S_FX;
  1239. else
  1240. { this is in the case where the instruction is LEA }
  1241. { or something like that, in that case size is not }
  1242. { important. }
  1243. instr.operands[operandnum].size := S_NO;
  1244. end; { end case }
  1245. end;
  1246. { ok, finished for this var }
  1247. CreateVarInstr := TRUE;
  1248. Exit;
  1249. end;
  1250. typedconstsym : begin
  1251. { we always assume in asm statements that }
  1252. { that the variable is valid. }
  1253. instr.operands[operandnum].ref.symbol:=newasmsymbol(pvarsym(sym)^.mangledname);
  1254. { the current size is NOT overriden if it already }
  1255. { exists, such as in the case of a byte ptr, in }
  1256. { front of the identifier. }
  1257. if (instr.operands[operandnum].size = S_NO) or (instr.operands[operandnum].overriden = FALSE) then
  1258. Begin
  1259. case ptypedconstsym(sym)^.getsize of
  1260. 1: instr.operands[operandnum].size := S_B;
  1261. 2: instr.operands[operandnum].size := S_W{ could be S_IS};
  1262. 4: instr.operands[operandnum].size := S_L{ could be S_IL or S_FS};
  1263. 8: instr.operands[operandnum].size := S_IQ{ could be S_D or S_FL};
  1264. extended_size: instr.operands[operandnum].size := S_FX;
  1265. else
  1266. instr.operands[operandnum].size := S_NO;
  1267. end; { end case }
  1268. end;
  1269. { ok, finished for this var }
  1270. CreateVarInstr := TRUE;
  1271. Exit;
  1272. end;
  1273. constsym : begin
  1274. if pconstsym(sym)^.consttype in [constint,constchar,constbool] then
  1275. begin
  1276. instr.operands[operandnum].operandtype:=OPR_CONSTANT;
  1277. instr.operands[operandnum].val:=pconstsym(sym)^.value;
  1278. CreateVarInstr := TRUE;
  1279. Exit;
  1280. end;
  1281. end;
  1282. typesym : begin
  1283. if ptypesym(sym)^.definition^.deftype in [recorddef,objectdef] then
  1284. begin
  1285. instr.operands[operandnum].operandtype:=OPR_CONSTANT;
  1286. instr.operands[operandnum].val:=0;
  1287. CreateVarInstr := TRUE;
  1288. Exit;
  1289. end;
  1290. end;
  1291. procsym : begin
  1292. instr.operands[operandnum].operandtype:=OPR_SYMBOL;
  1293. instr.operands[operandnum].symbol:=newasmsymbol(pprocsym(sym)^.definition^.mangledname);
  1294. CreateVarInstr := TRUE;
  1295. Exit;
  1296. end
  1297. else
  1298. begin
  1299. Message(assem_e_unsupported_symbol_type);
  1300. exit;
  1301. end;
  1302. end;
  1303. end;
  1304. { now check for parameters passed to routine }
  1305. if assigned(aktprocsym^.definition^.parast) then
  1306. sym:=aktprocsym^.definition^.parast^.search(hs)
  1307. else
  1308. sym:=nil;
  1309. if assigned(sym) then
  1310. begin
  1311. case sym^.typ of
  1312. varsym : begin
  1313. if pvarsym(sym)^.islocalcopy then
  1314. l:=-pvarsym(sym)^.address
  1315. else
  1316. begin
  1317. l:=pvarsym(sym)^.address;
  1318. { set offset }
  1319. inc(l,aktprocsym^.definition^.parast^.call_offset);
  1320. end;
  1321. pvarsym(sym)^.is_valid:=1;
  1322. inc(pvarsym(sym)^.refs);
  1323. instr.operands[operandnum].ref.base := procinfo.framepointer;
  1324. instr.operands[operandnum].ref.offset := l;
  1325. { the current size is NOT overriden if it already }
  1326. { exists, such as in the case of a byte ptr, in }
  1327. { front of the identifier. }
  1328. if (instr.operands[operandnum].size = S_NO) or (instr.operands[operandnum].overriden = FALSE) then
  1329. Begin
  1330. case pvarsym(sym)^.getsize of
  1331. 1: instr.operands[operandnum].size := S_B;
  1332. 2: instr.operands[operandnum].size := S_W;
  1333. 4: instr.operands[operandnum].size := S_L;
  1334. 8: instr.operands[operandnum].size := S_IQ;
  1335. extended_size: instr.operands[operandnum].size := S_FX;
  1336. else
  1337. { this is in the case where the instruction is LEA }
  1338. { or something like that, in that case size is not }
  1339. { important. }
  1340. instr.operands[operandnum].size := S_NO;
  1341. end; { end case }
  1342. end; { endif }
  1343. CreateVarInstr := TRUE;
  1344. Exit;
  1345. end;
  1346. else
  1347. begin
  1348. Message(assem_e_unsupported_symbol_type);
  1349. exit;
  1350. end;
  1351. end; { case }
  1352. end; { endif }
  1353. end;
  1354. { not found.. .now look for global variables. }
  1355. getsym(hs,false);
  1356. sym:=srsym;
  1357. if assigned(sym) then
  1358. Begin
  1359. case sym^.typ of
  1360. varsym,
  1361. typedconstsym : Begin
  1362. if sym^.typ=varsym then
  1363. inc(pvarsym(sym)^.refs);
  1364. instr.operands[operandnum].ref.symbol:=newasmsymbol(sym^.mangledname);
  1365. { the current size is NOT overriden if it already }
  1366. { exists, such as in the case of a byte ptr, in }
  1367. { front of the identifier. }
  1368. if (instr.operands[operandnum].size = S_NO) or (instr.operands[operandnum].overriden = FALSE) then
  1369. Begin
  1370. case pvarsym(sym)^.getsize of
  1371. 1: instr.operands[operandnum].size := S_B;
  1372. 2: instr.operands[operandnum].size := S_W;
  1373. 4: instr.operands[operandnum].size := S_L;
  1374. 8: instr.operands[operandnum].size := S_IQ;
  1375. else
  1376. { this is in the case where the instruction is LEA }
  1377. { or something like that, in that case size is not }
  1378. { important. }
  1379. instr.operands[operandnum].size := S_NO;
  1380. end;
  1381. end
  1382. else
  1383. if (instr.operands[operandnum].size = S_NO) and (sym^.typ = typedconstsym) then
  1384. Begin
  1385. { only these are valid sizes, otherwise prefixes are }
  1386. { required. }
  1387. case ptypedconstsym(sym)^.definition^.size of
  1388. 1: instr.operands[operandnum].size := S_B;
  1389. 2: instr.operands[operandnum].size := S_W;
  1390. 4: instr.operands[operandnum].size := S_L;
  1391. 8: instr.operands[operandnum].size := S_IQ;
  1392. else
  1393. { this is in the case where the instruction is LEA }
  1394. { or something like that, in that case size is not }
  1395. { important. }
  1396. instr.operands[operandnum].size := S_NO;
  1397. end;
  1398. end; { endif }
  1399. CreateVarInstr := TRUE;
  1400. Exit;
  1401. end;
  1402. constsym : begin
  1403. if pconstsym(sym)^.consttype in [constint,constchar,constbool] then
  1404. begin
  1405. instr.operands[operandnum].operandtype:=OPR_CONSTANT;
  1406. instr.operands[operandnum].val:=pconstsym(sym)^.value;
  1407. CreateVarInstr := TRUE;
  1408. Exit;
  1409. end;
  1410. end;
  1411. typesym : begin
  1412. if ptypesym(sym)^.definition^.deftype in [recorddef,objectdef] then
  1413. begin
  1414. instr.operands[operandnum].operandtype:=OPR_CONSTANT;
  1415. instr.operands[operandnum].val:=0;
  1416. CreateVarInstr := TRUE;
  1417. Exit;
  1418. end;
  1419. end;
  1420. procsym : begin
  1421. if assigned(pprocsym(sym)^.definition^.nextoverloaded) then
  1422. Message(assem_w_calling_overload_func);
  1423. instr.operands[operandnum].operandtype:=OPR_SYMBOL;
  1424. instr.operands[operandnum].size:=S_L;
  1425. instr.operands[operandnum].symbol:=newasmsymbol(pprocsym(sym)^.definition^.mangledname);
  1426. CreateVarInstr := TRUE;
  1427. Exit;
  1428. end;
  1429. else
  1430. begin
  1431. Message(assem_e_unsupported_symbol_type);
  1432. exit;
  1433. end;
  1434. end; {case}
  1435. end; { end looking for global variables .. }
  1436. end;
  1437. Function SearchLabel(const s: string; var hl: plabel): boolean;
  1438. {**********************************************************************}
  1439. { Description: Searches for a pascal label definition, first in the }
  1440. { local symbol list and then in the global symbol list. If found then }
  1441. { return pointer to label and return true, otherwise returns false. }
  1442. {**********************************************************************}
  1443. var
  1444. sym: psym;
  1445. Begin
  1446. SearchLabel := FALSE;
  1447. if assigned(aktprocsym) then
  1448. Begin
  1449. { Check the local constants }
  1450. if assigned(aktprocsym^.definition) then
  1451. Begin
  1452. if assigned(aktprocsym^.definition^.localst) then
  1453. sym := aktprocsym^.definition^.localst^.search(s)
  1454. else
  1455. sym := nil;
  1456. if assigned(sym) then
  1457. Begin
  1458. if (sym^.typ = labelsym) then
  1459. Begin
  1460. hl:=plabelsym(sym)^.number;
  1461. SearchLabel := TRUE;
  1462. exit;
  1463. end;
  1464. end;
  1465. end;
  1466. end;
  1467. { Check the global label symbols... }
  1468. getsym(s,false);
  1469. if srsym <> nil then
  1470. Begin
  1471. if (srsym^.typ=labelsym) then
  1472. Begin
  1473. hl:=plabelsym(srsym)^.number;
  1474. SearchLabel:= TRUE;
  1475. exit;
  1476. end;
  1477. end;
  1478. end;
  1479. {*************************************************************************}
  1480. { Instruction Generation Utilities }
  1481. {*************************************************************************}
  1482. Procedure ConcatString(p : paasmoutput;s:string);
  1483. {*********************************************************************}
  1484. { PROCEDURE ConcatString(s:string); }
  1485. { Description: This routine adds the character chain pointed to in }
  1486. { s to the instruction linked list. }
  1487. {*********************************************************************}
  1488. Var
  1489. pc: PChar;
  1490. Begin
  1491. getmem(pc,length(s)+1);
  1492. p^.concat(new(pai_string,init_length_pchar(strpcopy(pc,s),length(s))));
  1493. end;
  1494. Procedure ConcatPasString(p : paasmoutput;s:string);
  1495. {*********************************************************************}
  1496. { PROCEDURE ConcatPasString(s:string); }
  1497. { Description: This routine adds the character chain pointed to in }
  1498. { s to the instruction linked list, contrary to ConcatString it }
  1499. { uses a pascal style string, so it conserves null characters. }
  1500. {*********************************************************************}
  1501. Begin
  1502. p^.concat(new(pai_string,init(s)));
  1503. end;
  1504. Procedure ConcatDirect(p : paasmoutput;s:string);
  1505. {*********************************************************************}
  1506. { PROCEDURE ConcatDirect(s:string) }
  1507. { Description: This routine output the string directly to the asm }
  1508. { output, it is only sed when writing special labels in AT&T mode, }
  1509. { and should not be used without due consideration, since it may }
  1510. { cause problems. }
  1511. {*********************************************************************}
  1512. Var
  1513. pc: PChar;
  1514. Begin
  1515. getmem(pc,length(s)+1);
  1516. p^.concat(new(pai_direct,init(strpcopy(pc,s))));
  1517. end;
  1518. Procedure ConcatConstant(p: paasmoutput; value: longint; maxvalue: longint);
  1519. {*********************************************************************}
  1520. { PROCEDURE ConcatConstant(value: longint; maxvalue: longint); }
  1521. { Description: This routine adds the value constant to the current }
  1522. { instruction linked list. }
  1523. { maxvalue -> indicates the size of the data to initialize: }
  1524. { $ff -> create a byte node. }
  1525. { $ffff -> create a word node. }
  1526. { $ffffffff -> create a dword node. }
  1527. {*********************************************************************}
  1528. Begin
  1529. if value > maxvalue then
  1530. Begin
  1531. Message(assem_e_constant_out_of_bounds);
  1532. { assuming a value of maxvalue }
  1533. value := maxvalue;
  1534. end;
  1535. if maxvalue = $ff then
  1536. p^.concat(new(pai_const,init_8bit(byte(value))))
  1537. else
  1538. if maxvalue = $ffff then
  1539. p^.concat(new(pai_const,init_16bit(word(value))))
  1540. else
  1541. if maxvalue = $ffffffff then
  1542. p^.concat(new(pai_const,init_32bit(longint(value))));
  1543. end;
  1544. Procedure ConcatRealConstant(p : paasmoutput;value: bestreal; real_typ : tfloattype);
  1545. {***********************************************************************}
  1546. { PROCEDURE ConcatRealConstant(value: bestreal; real_typ : tfloattype); }
  1547. { Description: This routine adds the value constant to the current }
  1548. { instruction linked list. }
  1549. { real_typ -> indicates the type of the real data to initialize: }
  1550. { s32real -> create a single node. }
  1551. { s64real -> create a double node. }
  1552. { s80real -> create an extended node. }
  1553. { s64bit -> create a comp node. }
  1554. { f32bit -> create a fixed node. (not used normally) }
  1555. {***********************************************************************}
  1556. Begin
  1557. case real_typ of
  1558. s32real : p^.concat(new(pai_single,init(value)));
  1559. s64real : p^.concat(new(pai_double,init(value)));
  1560. s80real : p^.concat(new(pai_extended,init(value)));
  1561. s64bit : p^.concat(new(pai_comp,init(value)));
  1562. f32bit : p^.concat(new(pai_const,init_32bit(trunc(value*$10000))));
  1563. end;
  1564. end;
  1565. Procedure ConcatLabel(p: paasmoutput;var l : plabel);
  1566. {*********************************************************************}
  1567. { PROCEDURE ConcatLabel }
  1568. { Description: This routine either emits a label or a labeled }
  1569. { instruction to the linked list of instructions. }
  1570. {*********************************************************************}
  1571. begin
  1572. p^.concat(new(pai_label,init(l)))
  1573. end;
  1574. procedure ConcatPublic(p:paasmoutput;const s : string);
  1575. {*********************************************************************}
  1576. { PROCEDURE ConcatPublic }
  1577. { Description: This routine emits an global definition to the }
  1578. { linked list of instructions.(used by AT&T styled asm) }
  1579. {*********************************************************************}
  1580. begin
  1581. p^.concat(new(pai_symbol,init_global(s)));
  1582. { concat_internal(s,EXT_NEAR); done in aasm }
  1583. end;
  1584. procedure ConcatLocal(p:paasmoutput;const s : string);
  1585. {*********************************************************************}
  1586. { PROCEDURE ConcatLocal }
  1587. { Description: This routine emits an local definition to the }
  1588. { linked list of instructions. }
  1589. {*********************************************************************}
  1590. begin
  1591. p^.concat(new(pai_symbol,init(s)));
  1592. { concat_internal(s,EXT_NEAR); done in aasm }
  1593. end;
  1594. Procedure ConcatGlobalBss(const s : string;size : longint);
  1595. {*********************************************************************}
  1596. { PROCEDURE ConcatGlobalBss }
  1597. { Description: This routine emits an global datablock to the }
  1598. { linked list of instructions. }
  1599. {*********************************************************************}
  1600. begin
  1601. bsssegment^.concat(new(pai_datablock,init_global(s,size)));
  1602. { concat_internal(s,EXT_NEAR); done in aasm }
  1603. end;
  1604. Procedure ConcatLocalBss(const s : string;size : longint);
  1605. {*********************************************************************}
  1606. { PROCEDURE ConcatLocalBss }
  1607. { Description: This routine emits a local datablcok to the }
  1608. { linked list of instructions. }
  1609. {*********************************************************************}
  1610. begin
  1611. bsssegment^.concat(new(pai_datablock,init(s,size)));
  1612. { concat_internal(s,EXT_NEAR); done in aasm }
  1613. end;
  1614. { add to list of external labels }
  1615. Procedure ConcatExternal(const s : string;typ : texternal_typ);
  1616. {*********************************************************************}
  1617. { PROCEDURE ConcatExternal }
  1618. { Description: This routine emits an external definition to the }
  1619. { linked list of instructions.(used by AT&T styled asm) }
  1620. {*********************************************************************}
  1621. { check if in internal list and remove it there }
  1622. var p : pai_external;
  1623. begin
  1624. p:=search_assembler_symbol(internals,s,typ);
  1625. if p<>nil then internals^.remove(p);
  1626. concat_external(s,typ);
  1627. end;
  1628. { add to internal list of labels }
  1629. Procedure ConcatInternal(const s : string;typ : texternal_typ);
  1630. {*********************************************************************}
  1631. { PROCEDURE ConcatInternal }
  1632. { Description: This routine emits an internal definition of a symbol }
  1633. { (used by AT&T styled asm for undefined labels) }
  1634. {*********************************************************************}
  1635. begin
  1636. concat_internal(s,typ);
  1637. end;
  1638. end.
  1639. {
  1640. $Log$
  1641. Revision 1.6 1999-03-01 13:22:25 pierre
  1642. * varsym refs incremented
  1643. Revision 1.5 1999/02/25 21:02:51 peter
  1644. * ag386bin updates
  1645. + coff writer
  1646. Revision 1.4 1999/02/22 02:15:39 peter
  1647. * updates for ag386bin
  1648. Revision 1.3 1999/02/16 00:47:28 peter
  1649. * fixed local copies of value para's
  1650. Revision 1.2 1999/01/27 13:04:11 pierre
  1651. * bug with static vars in assembler readers
  1652. Revision 1.1 1999/01/10 15:38:00 peter
  1653. * moved some tables from ra386*.pas -> i386.pas
  1654. + start of coff writer
  1655. * renamed asmutils unit to rautils
  1656. }