<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0cm;
font-size:11.0pt;
font-family:"Calibri",sans-serif;
mso-fareast-language:EN-US;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:#0563C1;
text-decoration:underline;}
span.EmailStyle17
{mso-style-type:personal-compose;
font-family:"Calibri",sans-serif;
color:windowtext;}
.MsoChpDefault
{mso-style-type:export-only;
font-family:"Calibri",sans-serif;
mso-fareast-language:EN-US;}
@page WordSection1
{size:612.0pt 792.0pt;
margin:70.85pt 70.85pt 2.0cm 70.85pt;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="DE" link="#0563C1" vlink="#954F72" style="word-wrap:break-word">
<div class="WordSection1">
<p class="MsoNormal"><span lang="EN-GB">Hello everyone,<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">for testing our code I wanted to create an infinite loop with Clang, so I wrote the most basic infinite loop I came up with:<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">void Sample()<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">{<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB"> while(true);<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">}<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">To my surprise Clang generated the following assembly code:<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB"> .def "?Sample@@YAXXZ";<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB"> .scl 2;<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB"> .type 32;<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB"> .endef<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB"> .section .text,"xr",one_only,"?Sample@@YAXXZ"<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB"> .globl "?Sample@@YAXXZ" # -- Begin function ?Sample@@YAXXZ<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB"> .p2align 4, 0x90<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">"?Sample@@YAXXZ": # @"?Sample@@YAXXZ"<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">.Lfunc_begin4:<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB"> .cv_func_id 6<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB"># %bb.0:<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB"> nop # avoids zero-length function<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">.Lfunc_end4:<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB"> # -- End function<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">Which ended in an access violation D:<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">Why was Clang doing that? Is it because of some loop optimization since the body of the loop is empty and nothing else happens in the loop? :0<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">But why was it missing the "ret" instruction then?<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">The full command line without include directories is:<br>
1>clang version 13.0.0 (<a href="https://github.com/llvm/llvm-project.git">https://github.com/llvm/llvm-project.git</a> 5f500d73cd1aaff4c9ab2fd5c327c2d5ca9ae5c9)<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">1>Target: x86_64-pc-windows-msvc<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">1>Thread model: posix<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">1> "clang-cl.exe" -cc1 -triple x86_64-pc-windows-msvc19.28.29912 -S -disable-free -disable-llvm-verifier -discard-value-names -main-file-name CMException.cpp -mrelocation-model pic -pic-level 2 -mframe-pointer=none -relaxed-aliasing
-fmath-errno -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu skylake -target-feature +fsgsbase -mllvm -x86-asm-syntax=intel -mllvm -treat-scalable-fixed-error-as-warning -D_MT -flto-visibility-public-std --dependent-lib=libcmt --dependent-lib=oldnames
-stack-protector 2 -fcxx-exceptions -fexceptions -fexternc-nounwind -fms-volatile -fdefault-calling-conv=cdecl -fdiagnostics-format msvc -gno-column-info -gcodeview -debug-info-kind=limited -v -ffunction-sections -D WIN32 -D NTSYSAD_CM -D CODEMODULE -D _CONSOLE
-D _UNICODE -D NDEBUG -D _CRT_SECURE_NO_WARNINGS -D _CRT_USE_BUILTIN_OFFSETOF -D NOMINMAX -D _UNICODE -D UNICODE -O2 -Wall -Wno-error -Wno-unused-command-line-argument -Wno-microsoft-cast -Wno-writable-strings -Wno-microsoft-enum-forward-reference -Wno-invalid-token-paste
-fdeprecated-macro -ferror-limit 3000 -fno-use-cxa-atexit -fms-extensions -fms-compatibility -fms-compatibility-version=19.28.29912 -std=c++17 -fdelayed-template-parsing -fno-caret-diagnostics -vectorize-loops -vectorize-slp -faddrsig -o "x64\\Release\\CMException.asm"
-x c++ CMException.cpp<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">Thank you for any help in advance!<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">Kind regards<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">Björn<o:p></o:p></span></p>
</div>
Als GmbH eingetragen im Handelsregister Bad Homburg v.d.H. HRB 9816, USt.ID-Nr. DE 114 165 789 Geschäftsführer: Dr. Hiroshi Nakamura, Dr. Robert Plank, Markus Bode, Takashi Nagano, Tetsuhiro Habe, Ergin Cansiz.
</body>
</html>