Path: csiph.com!weretis.net!feeder6.news.weretis.net!news.misty.com!news.iecc.com!.POSTED.news.iecc.com!nerds-end From: antispam@math.uni.wroc.pl Newsgroups: comp.compilers Subject: Re: Implementing built-in functions with LLVM, help needed Date: Thu, 6 Oct 2022 15:15:44 -0000 (UTC) Organization: Aioe.org NNTP Server Sender: news@iecc.com Approved: comp.compilers@iecc.com Message-ID: <22-10-020@comp.compilers> References: <22-10-019@comp.compilers> Injection-Info: gal.iecc.com; posting-host="news.iecc.com:2001:470:1f07:1126:0:676f:7373:6970"; logging-data="58896"; mail-complaints-to="abuse@iecc.com" Keywords: Pascal, design Posted-Date: 07 Oct 2022 13:04:15 EDT X-submission-address: compilers@iecc.com X-moderator-address: compilers-request@iecc.com X-FAQ-and-archives: http://compilers.iecc.com Xref: csiph.com comp.compilers:3191 Ivan Espinosa wrote: > Hello, > > I'm currently working on a personal project about a front-end compiler > for some sort of the old Pascal version to produce LLVM code. I'm kind > of new to this amazing topic, and I would like to know how should I > implement functions like writeln, read, etc. It would be very useful > if somebody could help me with this giving some ideas or resources so > I can work in that. Built in functions in Pascal have special syntax, so you need approprate code in parser to recognize them. Easy trick is to have more general parsing for _all_ functions, but generate error after parsing when non-builtin function tries to use syntax reserved for builtins. Writeln, etc. allow variable number of arguments, but meaning is defined be equvalent sequence of one-argument calls. Your compiler needs to effectively replace single multiargument call by seqence of one or two argument calls. Basic fixed argument functions can be written in C or Pascal (if you have extentions to do system calls or calls to C library). There is extra difficulty: standard Pascal does not allow overloading for user defined functions, but buitin functions are overloaded. You can solve this in ad-hoc manner, having table which for each combination of arguments to builtin functions gives corresponding function in runtime library (in this apprach functions in runtime library have different names than builtins). This is essentially how GNU Pascal handles builtin calls. More elegant would be to support function/procedure overloading as compiler extention and write runtime library in extended language. However, in classic Pascal there is only handful of builtion functions so ad-hoc apprach _may_ be easier. OTOH IME implementing some feature inside compiler takes 5-10 times as much work as putting it in runtime library (when possible), and if you had overloading adding builtins would be much easier. Implementing general overloading requires some effort, depending on number of builtins and on specific implementaion choices it may be less or more work than ad-hoc approach to builtins. -- Waldek Hebisch