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

扶醉桌前 提交于 2019-12-29 07:17:10

问题


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 directives (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

回答1:


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.)




回答2:


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;
}


来源:https://stackoverflow.com/questions/1709850/conditional-compilation-in-assembler-s-code-for-iphone-how

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!