Stefan Bobev
Stefan Bobev

Reputation: 171

Producing a cmp and cmov instruction in V8 for x64

I am trying to implement my own version of essentially Math.clamp within V8 using the internal assembler.

I managed to produce a version of it using the Torque scripting language, and wanted to try doing a similar implementation using the CodeStubAssembler builtins. I realise that they are considered old school, but this seemed the easiest way to generate the actual assembly that I wanted. The goal is to make the compiler generate code as similar to this as possible

The question is: is there an assembler routine that already produces cmp and cmov instructions? If not, how would I go about generating this?

This is only for educational purposes, which is why I am only interested in the x64 pipeline and treat all inputs as integers.

Upvotes: 2

Views: 293

Answers (1)

jmrk
jmrk

Reputation: 40571

V8 developer here. The CodeStubAssembler is not designed or intended to let you control precisely what assembly instructions are emitted -- it is platform-independent, that's a big part of its reason to exist.

You can look at MathBuiltinsAssembler::MathMaxMin in src/builtins/builtins-math-gen.cc and its two call sites further down in that same file to see how you would express a sequence of min/max operations (or other conditional selections) in CSA. It is then up to the Turbofan backend powering CSA to select appropriate machine instructions on each platform. If you think that the machine code could be improved, then the way to address that would be to improve Turbofan's instruction selector (and/or earlier optimization passes) to detect the relevant patterns.

If you actually want to write platform-specific assembly by hand, you can do that too: instead of the CodeStubAssembler, you'd use the platform-specific MacroAssembler, which (on x64) exposes both cmp and cmov. There are a few such builtins left that you can study as examples, look in src/builtins/x64/builtins-x64.cc. There used to be more of these, but we have migrated almost all of them to other implementation techniques, like C++, CSA, or Torque, because reading/writing/debugging/maintaining assembly code is time-consuming and error-prone.

Upvotes: 7

Related Questions