Received: from eggs.gnu.org ([2001:4830:134:3::10]:52974) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1admc9-0006oZ-9V for qemu-devel@nongnu.org; Wed, 09 Mar 2016 17:29:58 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1admc6-0004gx-1l for qemu-devel@nongnu.org; Wed, 09 Mar 2016 17:29:57 -0500 Received: from roura.ac.upc.es ([147.83.33.10]:40400) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1admc5-0004gZ-LH for qemu-devel@nongnu.org; Wed, 09 Mar 2016 17:29:53 -0500 From: =?utf-8?Q?Llu=C3=ADs_Vilanova?= References: <87r3fjpvgx.fsf@fimbulvetr.bsc.es> <56E046D3.2080709@twiddle.net> <87twkfo6tn.fsf@fimbulvetr.bsc.es> <56E0714F.1090602@twiddle.net> Date: Wed, 09 Mar 2016 23:29:49 +0100 In-Reply-To: <56E0714F.1090602@twiddle.net> (Richard Henderson's message of "Wed, 9 Mar 2016 13:54:07 -0500") Message-ID: <878u1rnv36.fsf@fimbulvetr.bsc.es> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [Qemu-devel] [RFC] [tcg] Idea on refactoring target code generation loop (gen_intermediate_code) List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Richard Henderson Cc: Paolo Bonzini , qemu-devel@nongnu.org, Peter Crosthwaite Richard Henderson writes: > On 03/09/2016 01:16 PM, Llu=C3=ADs Vilanova wrote: >> Richard Henderson writes: >>=20 >>> On 03/09/2016 09:38 AM, Llu=C3=ADs Vilanova wrote: >>>> Hi, >>>>=20 >>>> NOTE: I won't be throwing patches anytime soon, I just want to know if= there's >>>> interest in this for the future. >>>>=20 >>>> While adding events for tracing guest instructions, I've found that the >>>> per-target "gen_intermediate_code()" function is very similar but not = exactly >>>> the same for each of the targets. This makes architecture-agnostic fea= tures >>>> harder to maintain across targets, specially when it comes to their re= lative >>>> order. >>>>=20 >>>> So, would it be worth it if I generalized part of that code into an >>>> architecture-agnostic function that calls into target-specific hooks w= herever it >>>> needs extending? There are many ways to do it that we can discuss late= r. >>=20 >>> It's worth talking about, since I do believe it would make long-term ma= intenance >>> across the targets easier. >>=20 >>> These "target-specific hooks" probably ought not be "hooks" in the >>> traditional sense of attaching them to CPUState. I'd be more comfortab= le with a >>> refactoring that used include files -- maybe .h or maybe .inc.c. If we= do the >>> normal sort of hook, then we've got to either expose DisasContext in pl= aces we >>> shouldn't, or dynamically allocate it. Neither seems particularly appe= aling. >>=20 >> Great. I pondered about using QOM vs "template code", and I'm leaning to= wards >> the latter: >>=20 >> * translate.c: >>=20 >> struct DisasContextArch { >> DisasContext common; >> }; >>=20 >> // implement "hooks" >>=20 >> void gen_intermediate_code(CPUArchState *env, TranslationBlock *tb) >> { >> DisasContextArch dc; >> gen_intermediate_code_template(get_cpu(env), &dc.common, tb); > Pointer down into "common" here... >> } >>=20 >> * translate-template.h: >>=20 >> struct DisasContext { /* ... */ }; >>=20 >> void gen_intermediate_code_template(CPUState *cpu, DisasContext *dc, >> TranslationBlock *tb) >> { >> // init dc >> // arch-specific init dc hook >>=20 >> // gen_icount, etc. >> while (true) { >> // generic processing code with calls to hooks > ... means you have to upcast to DisasContextArch here, or in the hooks th= emselves. Yup, that was the intention. Hooks should then use "container_of()" to get = their part. >> } >> } >>=20 >> While initially simpler, the "template code" still feels a little dirty = to >> me. With QOM, you could implement a DisasContextClass that provides the >> per-target hook pointers, which can be globally allocated once per targe= t and >> pointed to by the DisasContext allocated in stack. >>=20 >> I'm not sure about what you mean by exposing DisasContext in places it s= houldn't >> be, though. > You either split DisasContextArch / DisasContext in "odd" ways, and then = have to > cast back and forth, or you have to expose the whole of DisasContextArch.= It's > the latter that I considered inappropriate. Well, moving whatever is used in the generic code into DisasContext doesn't sound too odd as a first solution. > Alternately... can we broach the subject of C++? Honestly, it seems we w= ork too > hard sometimes to re-implement templates and classes in C. Whooo, I'd really *love* to switch to C++ just for templates and classes...= But last time this was discussed, the idea wasn't met with much joy :) Cheers, Lluis