<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">After few hours of research I think I have a clear picture what needs to be done. <div class=""><br class=""></div><div class="">The switch statement in <span style="font-family: Menlo; font-size: 11px; color: rgb(187, 44, 162);" class="">bool</span><span style="font-family: Menlo; font-size: 11px;" class=""> </span><span style="font-family: Menlo; font-size: 11px; color: rgb(79, 129, 135);" class="">SILParser</span><span style="font-family: Menlo; font-size: 11px;" class="">::parseSILInstruction(</span><span style="font-family: Menlo; font-size: 11px; color: rgb(79, 129, 135);" class="">SILBasicBlock</span><span style="font-family: Menlo; font-size: 11px;" class=""> *BB)</span> will be replaced with few lines of code similar to these:</div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><br class=""></div></div><span style="font-family: Menlo; font-size: 11px; color: rgb(79, 129, 135);" class="">SILInstructionParser</span><span style="font-family: Menlo; font-size: 11px;" class=""> InstructionParser(*</span><span style="font-family: Menlo; font-size: 11px; color: rgb(187, 44, 162);" class="">this</span><span style="font-family: Menlo; font-size: 11px;" class="">);</span><br class=""><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">ResultVal = InstructionParser.visit(Opcode);</div></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class="">There is going to be a new class with all visit methods:</div><div style="margin: 0px; line-height: normal;" class=""><div style="font-family: Menlo; font-size: 11px; margin: 0px; line-height: normal; color: rgb(187, 44, 162);" class="">namespace<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> {</span></div><div style="font-family: Menlo; font-size: 11px; margin: 0px; line-height: normal;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">class</span> SILInstructionParser</div><div style="font-family: Menlo; font-size: 11px; margin: 0px; line-height: normal; color: rgb(79, 129, 135);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> : </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">public</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>SILVisitor<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><</span>SILInstructionParser<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">, </span>ValueBase<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">> {</span></div><div style="font-family: Menlo; font-size: 11px; margin: 0px; line-height: normal; color: rgb(187, 44, 162);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>public<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">:</span></div><div style="font-family: Menlo; font-size: 11px; margin: 0px; line-height: normal;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">SILParser</span> &P;</div><p style="font-family: Menlo; font-size: 11px; margin: 0px; line-height: normal; min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><div style="font-family: Menlo; font-size: 11px; margin: 0px; line-height: normal;" class=""> SILInstructionParser(<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">SILParser</span> &P): <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">P</span>(P) {}</div><p style="font-family: Menlo; font-size: 11px; margin: 0px; line-height: normal; min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><div style="font-family: Menlo; font-size: 11px; margin: 0px; line-height: normal; color: rgb(0, 132, 0);" class="">// ValueBase visitSILArgument(ValueBase opcode) {</div><div style="font-family: Menlo; font-size: 11px; margin: 0px; line-height: normal; color: rgb(0, 132, 0);" class="">// llvm_unreachable("not an instruction");</div><div style="font-family: Menlo; font-size: 11px; margin: 0px; line-height: normal; color: rgb(0, 132, 0);" class="">// }</div><div style="font-family: Menlo; font-size: 11px; margin: 0px; line-height: normal; color: rgb(0, 132, 0);" class="">//</div><div style="font-family: Menlo; font-size: 11px; margin: 0px; line-height: normal; color: rgb(0, 132, 0);" class="">// ValueBase visitSILUndef(ValueBase opcode) {</div><div style="font-family: Menlo; font-size: 11px; margin: 0px; line-height: normal; color: rgb(0, 132, 0);" class="">// llvm_unreachable("not an instruction");</div><div style="font-family: Menlo; font-size: 11px; margin: 0px; line-height: normal; color: rgb(0, 132, 0);" class="">// }</div><div style="font-family: Menlo; font-size: 11px; margin: 0px; line-height: normal;" class=""> };</div><div style="font-family: Menlo; font-size: 11px; margin: 0px; line-height: normal; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">} </span>// end anonymous namespace</div><div style="font-family: Menlo; font-size: 11px; margin: 0px; line-height: normal; color: rgb(0, 132, 0);" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class="">And there is going to be a new method on SILVisitor, what will map opcode to the SILInstructionParser methods, something similar to:</div><div style="margin: 0px; line-height: normal;" class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> ValueRetTy visit(<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">ValueBase</span> *V) {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">switch</span> (V-><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">getKind</span>()) {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(120, 73, 42);" class="">#define VALUE(CLASS, PARENT) \</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(120, 73, 42);" class=""> case ValueKind::CLASS: \</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(120, 73, 42);" class=""> return asImpl().visit##CLASS(static_cast<CLASS*>(V));</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #78492a" class="">#include </span>"swift/SIL/SILNodes.def"</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #78492a" class="">llvm_unreachable</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(</span>"Not reachable, all cases handled"<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">);</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div></div></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class="">Is there a way how I can run only preprocessor, something similar to <span class="pun" style="white-space: inherit; margin: 0px; padding: 0px; border: 0px;">-</span><span class="pln" style="white-space: inherit; margin: 0px; padding: 0px; border: 0px;">E option for GCC? </span></div><div style="margin: 0px; line-height: normal;" class=""><span class="pln" style="white-space: inherit; margin: 0px; padding: 0px; border: 0px;">I’m getting build error with my changes and I would like to see the output for the SILVisitor class.</span></div><div style="margin: 0px; line-height: normal;" class=""><span class="pln" style="white-space: inherit; margin: 0px; padding: 0px; border: 0px;"><br class=""></span></div><div style="margin: 0px; line-height: normal;" class=""><span class="pln" style="white-space: inherit; margin: 0px; padding: 0px; border: 0px;">Sergey</span></div><div class=""><br class=""></div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On 14 Jan 2016, at 19:27, Michael Gottesman <<a href="mailto:mgottesman@apple.com" class="">mgottesman@apple.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">No I mean like this:<div class=""><br class=""></div><div class="">1. Add a visitor in SILVisitor.h that just switches on value base.</div><div class="">2. Define a composition class with SILParser:</div><div class=""><br class=""><div class=""><div class="">class SILInstructionParser {</div><div class=""> SILParser *P;</div><div class=""><br class=""></div><div class=""> ValueBase *visitValueBase() { llvm_unreachable("Unimplemented method"); }</div><div class=""> ValueBase *visitSILArgument.</div><div class="">};</div><div class=""><br class=""></div><div class="">...</div><div class=""><br class=""></div><div class="">SILValue SILInstructionParser::visitSILArgument() {</div><div class=""> ...</div><div class="">}</div><div class=""><br class=""></div><div class="">SILValue SILInstructionParser::visitAllocBoxInst() {</div><div class=""> ...</div><div class="">}</div><div class=""><br class=""></div><div class="">Michael</div><div class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Jan 14, 2016, at 9:00 AM, Sergey Bolshedvorsky <<a href="mailto:sergey@bolshedvorsky.com" class="">sergey@bolshedvorsky.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="content-type" content="text/html; charset=utf-8" class=""><div dir="auto" class=""><div class=""><div class="">Michael,</div><div class=""><br class=""></div><div class="">The simplest way is to keep the switch statement in the parseSILInstruction method and move all logic into parser routines. Is it right?</div><div class=""><br class=""></div><div class="">bool SILParser::parseSILInstruction(SILBasicBlock *BB) {</div><div class=""> // Header</div><div class=""><br class=""></div><div class=""> switch (Opcode) {</div><div class=""> case ValueKind::SILArgument: </div><div class=""> return handleValueKindSILArgument(Opcode); </div><div class=""> break;</div><div class=""> case ValueKind::AllocBoxInst: </div><div class=""> return handleValueKindAllocBoxInst(Opcode);</div><div class=""> break;</div><div class=""> case ValueKind::ApplyInst:</div><div class=""> return handleValueKindApplyInst(Opcode);</div><div class=""> break;</div><div class=""> // ...</div><div class=""> }</div><div class="">}</div><div class=""><br class=""></div><div class="">SILParser::handleValueKindSILArgument(ValueKind Opcode) {</div><div class=""> // Handle ValueKind::SILArgument case here</div><div class="">}</div><div class=""><br class=""></div><div class="">SILParser::handleValueKindAllocBoxInst(ValueKind Opcode) {</div><div class=""> // Handle ValueKind::AllocBoxInst case here</div><div class="">}</div><div class=""><br class=""></div><div class="">SILParser::handleValueKindApplyInst(ValueKind Opcode) {</div><div class=""> // Handle ValueKind::ApplyInst case here </div><div class="">}</div><div class=""><br class=""></div><div class="">By the way, I’ve noticed that some instructions are getting parsed by parser routines already: parseSILFunctionRef or parseCallInstruction.</div><div class=""><br class=""></div><div class="">Sergey</div><br class="">Sent from my iPhone</div><div class=""><br class="">On 14 Jan 2016, at 06:00, Michael Gottesman via swift-dev <<a href="mailto:swift-dev@swift.org" class="">swift-dev@swift.org</a>> wrote:<br class=""><br class=""></div><blockquote type="cite" class=""><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Jan 13, 2016, at 9:08 PM, Michael Gottesman via swift-dev <<a href="mailto:swift-dev@swift.org" class="">swift-dev@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><blockquote type="cite" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class="Apple-interchange-newline">On Jan 13, 2016, at 3:35 PM, John McCall via swift-dev <<a href="mailto:swift-dev@swift.org" class="">swift-dev@swift.org</a>> wrote:<br class=""><br class=""><blockquote type="cite" class="">On Jan 13, 2016, at 3:25 PM, Jordan Rose via swift-dev <<a href="mailto:swift-dev@swift.org" class="">swift-dev@swift.org</a>> wrote:<br class="">Hey, Sergey. It definitely makes sense to refactor this, but I don't think putting methods on SILInstruction is the way to go about it. There's no reason most clients of SIL need to know anything about parsing; from a separation-of-concerns perspective it belongs in a separate header and very likely a separate component (i.e. not lib/SIL/).<br class=""><br class="">If this were Swift, we could use an extension, but alas. :-)<br class=""><br class="">That said, I'm not a heavy user of SIL, so someone else from the Swift team with more of a stake should comment.<br class=""></blockquote><br class="">I agree with you completely.<br class=""><br class="">Also, the SIL instruction parser does not actually have an instance of an instruction to perform virtual dispatch on. Nor should we introduce the concept of “wireframe” instructions just for the convenience of the parser. Redundancy between cases here should be addressed with normal redundancy elimination techniques, i.e. macros and templates.<br class=""></blockquote><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">The way to do this is to have a visitor parser that takes in a ValueKind and maps it to the parser routine to use.</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""></div></blockquote><div class=""><br class=""></div><div class="">Let me rephrase. IIRC the way that this code is written is it first deserializes the value kind. Imagine if we had a visitor that was composed with the parser whose visitor methods would perform the relevant parsing.</div><br class=""><blockquote type="cite" class=""><div class=""><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">Michael</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><blockquote type="cite" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class="">John.<br class="">_______________________________________________<br class="">swift-dev mailing list<br class=""><a href="mailto:swift-dev@swift.org" class="">swift-dev@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-dev" class="">https://lists.swift.org/mailman/listinfo/swift-dev</a><br class=""></blockquote><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">_______________________________________________</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">swift-dev mailing list</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><a href="mailto:swift-dev@swift.org" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">swift-dev@swift.org</a><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><a href="https://lists.swift.org/mailman/listinfo/swift-dev" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">https://lists.swift.org/mailman/listinfo/swift-dev</a></div></blockquote></div><br class="">
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=hmIxNMeUeKq1PNSKRTOoag-2BAEfT4iqdAHk7XD4YDHG6eTsM31HYaPE6C6WaYV5XF32DQ3MHVaDvNKugsnhDiODx4BlwkzdGrpOEuIEktokN8NyYNQhhXFxcG6UqqsS2OeLe22IlstjV5YxUPGT4UcvqUq4prklmJCA8MV0qylTQspgNOgv69paGtKOmClTeYU804T-2B-2B09tfXaR1RHX4N9qTu3DO12A2uN2-2Fk2hWupoM-3D" alt="" width="1" height="1" border="0" style="height:1px !important;width:1px !important;border-width:0 !important;margin-top:0 !important;margin-bottom:0 !important;margin-right:0 !important;margin-left:0 !important;padding-top:0 !important;padding-bottom:0 !important;padding-right:0 !important;padding-left:0 !important;" class="">
</div></blockquote><blockquote type="cite" class=""><div class=""><span class="">_______________________________________________</span><br class=""><span class="">swift-dev mailing list</span><br class=""><span class=""><a href="mailto:swift-dev@swift.org" class="">swift-dev@swift.org</a></span><br class=""><span class=""><a href="https://lists.swift.org/mailman/listinfo/swift-dev" class="">https://lists.swift.org/mailman/listinfo/swift-dev</a></span><br class=""></div></blockquote></div></div></blockquote></div><br class=""></div></div></div></div></div></blockquote></div><br class=""></div></body></html>