{"id":5327,"date":"2025-01-24T10:24:39","date_gmt":"2025-01-24T09:24:39","guid":{"rendered":"https:\/\/www.islandwalking.com\/wordpress\/?p=5327"},"modified":"2025-01-24T10:25:48","modified_gmt":"2025-01-24T09:25:48","slug":"llvm-exercise-ii","status":"publish","type":"post","link":"https:\/\/www.islandwalking.com\/wordpress\/llvm-exercise-ii\/","title":{"rendered":"LLVM Exercise II"},"content":{"rendered":"<p>A tiny addition to the empty function. Returning zero:<\/p>\n<blockquote>\n<pre>int foo() {\r\n    return 0;\r\n}\r\n<\/pre>\n<\/blockquote>\n<p>Remember, we will not care about returning values to the calculator registers (at not least for now). We will only generate functions callable from MCODE. So as a convention we will choose to return values in the C register.<\/p>\n<p>So we will need an instruction to load <code>0<\/code> into the C register:<\/p>\n<blockquote>\n<pre>def Ceq0ALL : HP41MCODEInst&lt;0x04E, (outs RC:$r), (ins),\r\n                            \"C=0 ALL\", [(set RC:$r, 0)]&gt;;\r\n<\/pre>\n<\/blockquote>\n<p>This replacement pattern will make sure a constant <code>0<\/code> is loaded with this instruction:<\/p>\n<blockquote>\n<pre>def : Pat&lt;(i32 0), (Ceq0ALL)&gt;;\r\n<\/pre>\n<\/blockquote>\n<p>A register class for just this register (the instruction is only used with the C register, i.e. implicit addressing):<\/p>\n<blockquote>\n<pre>def RC : RegisterClass&lt;\"HP41MCODE\", [i32], 8, (add C)&gt;;<\/pre>\n<\/blockquote>\n<p>Defining the calling convention to return values in the C register:<\/p>\n<blockquote>\n<pre>def RetCC_HP41MCODE : CallingConv&lt;[\r\n      CCAssignToReg&lt;[C]&gt;\r\n    ]&gt;;\r\n<\/pre>\n<\/blockquote>\n<p>Adding this calling convention to the <code>LowerReturn()<\/code> function mentioned earlier, to analyze returns:<\/p>\n<blockquote>\n<pre>SDValue\r\nHP41MCODETargetLowering::LowerReturn(SDValue Chain,\r\n        CallingConv::ID CallConv,\r\n        bool IsVarArg,\r\n        const SmallVectorImpl &amp;Outs,\r\n        const SmallVectorImpl &amp;OutVals,\r\n        const SDLoc &amp;DL, SelectionDAG &amp;DAG) const {\r\n  ...\r\n  \/\/ Analyze return values.\r\n  CCInfo.AnalyzeReturn(Outs, RetCC_HP41MCODE);\r\n  ...\r\n  return DAG.getNode(HP41MCODEISD::RTN, DL, MVT::Other, RetOps);\r\n}\r\n<\/pre>\n<\/blockquote>\n<p>That gives us this MCODE assembly:<\/p>\n<blockquote>\n<pre>\t.file\t\"hello.c\"\r\n\t.text\r\n\t.globl\tfoo                     ! -- Begin function foo\r\n\t.type\tfoo,@function\r\nfoo:                                    ! @foo\r\n! %bb.0:                                ! %entry\r\n\tC=0 ALL\r\n\tRTN\r\n.Lfunc_end0:\r\n\t.size\tfoo, .Lfunc_end0-foo\r\n                                        ! -- End function\r\n\t.ident\t\"clang version 20.0.0git (https:\/\/github.com\/llvm\/llvm-project.git ea1dfd50bfdfbd75969fd7653bc71c81f2a2350f)\"\r\n\t.section\t\".note.GNU-stack\"\r\n\t.addrsig\r\n\r\n<\/pre>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>A tiny addition to the empty function. Returning zero: int foo() { return 0; } Remember, we will not care about returning values to the calculator registers (at not least for now). We will only generate functions callable from MCODE. So as a convention we will choose to return values in the C register. So &hellip; <a class=\"read-excerpt\" href=\"https:\/\/www.islandwalking.com\/wordpress\/llvm-exercise-ii\/\">Continue reading <span class=\"meta-nav\">&raquo;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5327","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.islandwalking.com\/wordpress\/wp-json\/wp\/v2\/posts\/5327","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.islandwalking.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.islandwalking.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.islandwalking.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.islandwalking.com\/wordpress\/wp-json\/wp\/v2\/comments?post=5327"}],"version-history":[{"count":5,"href":"https:\/\/www.islandwalking.com\/wordpress\/wp-json\/wp\/v2\/posts\/5327\/revisions"}],"predecessor-version":[{"id":5332,"href":"https:\/\/www.islandwalking.com\/wordpress\/wp-json\/wp\/v2\/posts\/5327\/revisions\/5332"}],"wp:attachment":[{"href":"https:\/\/www.islandwalking.com\/wordpress\/wp-json\/wp\/v2\/media?parent=5327"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.islandwalking.com\/wordpress\/wp-json\/wp\/v2\/categories?post=5327"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.islandwalking.com\/wordpress\/wp-json\/wp\/v2\/tags?post=5327"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}