开发者

Conditional Compilation in assembler (.s) code for iPhone - how?

开发者 https://www.devze.com 2022-12-10 21:07 出处:网络
I have a few lines of assembler arm code in an .s file. Just a few routines i need to call. It works fine when building for the device, however when i switch to iPhone Simulator i get \"no such instru

I have a few lines of assembler arm code in an .s file. Just a few routines i need to call. It works fine when building for the device, however when i switch to iPhone Simulator i get "no such instruction" errors. I tried to compile parts of the .s file conditionally with what i know:

#if !TARGET_IPHONE_SIMULATOR

But the assembler doesn't recognize these preprocessor directive开发者_开发知识库s (of course) and none of the conditional compilation techniques for assembler that i could remember or find worked, so i'm scratching my head now on how to avoid compilation of that assembler code when building for the Simulator. I also don't see a project option in Xcode that would allow me to compile the file or not depending on the target platform.

SOLVED:

All i was missing was the proper #import in the assembler file. I did not think of adding it because Xcode syntax highlighted any preprocessor directive in green (comment) which made me assume that these commands are not recognized when in fact they work just fine.

This works:

#import "TargetConditionals.h"

#if !TARGET_IPHONE_SIMULATOR

... asm code here ...

#endif


You do do it with a pre-processor macro. They are defined in TargetConditionals.h TARGET_IPHONE_SIMULATOR should be there! (You do need to #include it however.)


Here is code I use to detect ARM vs Thumb vs Simulator:

#include "TargetConditionals.h"

#if defined(__arm__)
# if defined(__thumb__)
#  define COMPILE_ARM_THUMB_ASM 1
# else
#  define COMPILE_ARM_ASM 1
# endif
#endif

#if TARGET_IPHONE_SIMULATOR
  // Simulator defines
#else
  // ARM or Thumb mode defines
#endif

// And here is how you might use it

uint32_t
test_compare_shifted_operand(uint32_t w1) {
  uint32_t local;
#if defined(COMPILE_ARM_ASM)
  const uint32_t shifted = (1 << 8);
  __asm__ __volatile__ (
                        "mov %[w2], #1\n\t"
                        "cmp %[w2], %[w1], lsr #8\n\t"
                        "moveq %[w2], #10\n\t"
                        "movne %[w2], #11\n\t"
                        : \
                        [w1] "+l" (w1),
                        [w2] "+l" (local)
                        : \
                        [shifted] "l" (shifted)
                        );
#else // COMPILE_ARM_ASM
  if ((w1 >> 8) == 1) {
    local = 10;
  } else {
    local = 11;
  }
#endif // COMPILE_ARM_ASM  
  return local;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消